Showing posts with label Search by key=>value in a multidimensional array in PHP. Show all posts
Showing posts with label Search by key=>value in a multidimensional array in PHP. Show all posts

Wednesday, 2 December 2015

Search by key and value in a multidimensional array in PHP

Hello

Example of Search by key and value in a multidimensional array in PHP


I have to get Code How to search array it key value of array example here

function array_search_by_key($array, $key, $value)
{
    $results = array();

    if (is_array($array)) {
        if (isset($array[$key]) && $array[$key] == $value) {
            $results[] = $array;
        }

        foreach ($array as $subarray) {
            $results = array_merge($results, array_search_by_key($subarray, $key, $value));
        }
    }

    return $results;
}

$user = array(0 => array(id=>1,name=>"test1",address=>"add1",gendor=>"male"),
             1 => array(id=>2,name=>"test2",address=>"add2",gendor=>"female"),
             2 => array(id=>3,name=>"test3",address=>"add3",gendor=>"male"));

print_r(array_search_by_key($user, 'name', 'test2'));
print_r(array_search_by_key($user, 'id', '1'));
print_r(array_search_by_key($user, 'address', 'add3'));
print_r(array_search_by_key($user, 'gendor', 'male'));