开发者

Search for a value in a specific column in the second level of a multidimensional array [duplicate]

This question already has answers here: PHP multidimensional array search by valu开发者_如何学JAVAe (23 answers) Closed 4 days ago.
$array = array(
    'the-1'=> array('name'=>'lorem','pos'=>array('top'=>'90','left'=>'80'),'zindex'=>2),
    'the-2'=> array('name'=>'ipsum','pos'=>array('top'=>'190','left'=>'180'),'zindex'=>1),
    'the-3'=> array('name'=>'lorem ipsum','pos'=>array('top'=>'20','left'=>'30'),'zindex'=>3)
);

How to check zindex key exist in above php array.


You have a method called array_key_exists for that. Of course you need to do some (recursive) looping if you don't know how deep the array with the value is located.


Maybe you should thing on using array_walk_recursive function in this way

<?php
$sweet = array('a' => 'apple', 'b' => 'banana');
$fruits = array('sweet' => $sweet, 'sour' => 'lemon');

function test_print($item, $key)
{
    echo "$key holds $item\n";
}

array_walk_recursive($fruits, 'test_print');
?>

this is just a print example taken from php.net, but easily you can adapt it to fit your needs (adding the array_key_exist in the callback function, for example)


I'm not completely sure what you're wanting here, so here are a variety of tests you can run on zindex. These all use a foreach loop and array_key_exists.

If you want to check each item in the outer array to see if it has a zindex:

This loops through each element, and simply checks to see if the element has a zindex key of some sort.

foreach( $array as $key => $element ) {
    if( array_key_exists('zindex',$element) ) {
        echo "Key '$key' has a zindex of ".$element['zindex']."\n<br>\n";
    } else {
        echo "Fail!! Key '$key' has no zindex!\n<br>\n";
    }
}

If you are just looking for there to be any zindex key at all:

This loops through until it finds an element that has a zindex. If a zindex is found, the function returns true, otherwise it returns false.

function find_zindex( $array ) {
    foreach( $array as $key => $element ) {
        if( array_key_exists('zindex',$element) ) {
            echo "Key '$key' has a zindex of ".$element['zindex']."\n<br>\n";
            return true;
        }
    }
    return false;
}

if( find_zindex( $array ) ) {
    echo "A zindex was found\n<br>\n";
} else {
    echo "Fail: no zindex was found\n<br>\n";
}

If you're looking for a particular zindex value in your array:

This loops through, looking for a particular zindex that has a particular value. If it is found, then the key for the outside array is returned. Otherwise null is returned.

function find_zindex( $array, $search_key ) {
    foreach( $array as $key => $element ) {
        if( array_key_exists('zindex',$element) && $element['zindex']==$search_key ) {
            echo "Key '$key' has a zindex of ".$element['zindex']."\n<br>\n";
            return $key;
        }
    }
    return null;
}

$key = find_zindex( $array, 3 );
if( $key ) {
    echo "The zindex was found at '$key'\n<br>\n";
} else {
    echo "Fail: the zindex was not found\n<br>\n";
}

$key = find_zindex( $array, 4 );
if( $key ) {
    echo "The zindex was found at '$key'\n<br>\n";
} else {
    echo "Fail: the zindex was not found\n<br>\n";
}

If you want an array of every key that has a particular zindex:

This loops through, building an array that contains every element that matches the supplied zindex search value. When done, it returns the new array of elements. If nothing is found, it returns an empty array that will test as false.

function find_zindex( $array, $search_key ) {
    $result = array();
    foreach( $array as $key => $element ) {
        if( array_key_exists('zindex',$element) && $element['zindex']==$search_key ) {
            echo "Key '$key' has a zindex of ".$element['zindex']."\n<br>\n";
            $result[] = $key;
        }
    }
    return $result;
}

$key = find_zindex( $array, 3 );
if( $key ) {
    echo 'The zindex was found at:';
    print_r( $key );
    echo "\n<br>\n";
} else {
    echo "Fail: the zindex was not found\n<br>\n";
}

$key = find_zindex( $array, 4 );
if( $key ) {
    echo 'The zindex was found at:';
    print_r( $key );
    echo "\n<br>\n";
} else {
    echo "Fail: the zindex was not found\n<br>\n";
}

If you are often trying to find data by the zindex, you will want to redesign your array:

This creates a second array that just has references to the elements in the first array. If you run this, you can see that the data is shared because the one assignment will set 'name' to 'new_name' in both arrays. Notice that each element of the outer array now has both an index and a zindex.

This assumes every element in $array has a zindex and the value of the zindex is unique. If some elements don't have a zindex or have duplicate zindexes, then you will need to modify this some.

$array = array(
    'the-1'=> array('name'=>'lorem','pos'=>array('top'=>'90','left'=>'80'),'index'=>'the-1','zindex'=>2),
    'the-2'=> array('name'=>'ipsum','pos'=>array('top'=>'190','left'=>'180'),'index'=>'the-2','zindex'=>1),
    'the-3'=> array('name'=>'lorem ipsum','pos'=>array('top'=>'20','left'=>'30'),'index'=>'the-3','zindex'=>3)
);

$zarray = array();
foreach( $array as &$value ) {
    $zarray[$value['zindex']] =& $value;
}
// optional: order the entries in zarray by their key
ksort($zarray)

print_r($array);
echo "\n<br>\n";
print_r($zarray);
echo "\n<br>\n<br>\n<br>\n<br>\n<br>\n";

$array['the-1']['name']='new_name';
print_r($array);
echo "\n<br>\n";
print_r($zarray);


Here's my solution:

function find_value_by_key($key,$array) {
        $data = array('key'=>$key,'ret'=>false);
        array_walk_recursive($array,function($v,$k) use (&$data) {
            if ($data['ret'])
                return;
            if ($k==$data['key'] and $v)
                $data['ret'] = $v;
        },$data);
        return $data['ret'];
}

In this case it returns the first value it finds (in your case it would return 2 when searching for "zindex"), if you need all the value returned in an array you can use this function:

function find_value_by_key($key,$array) {
    $data = array('key'=>$key,'ret'=>array());
    array_walk_recursive($array,function($v,$k) use (&$data) {
        if ($k==$data['key'] and $v)
            $data['ret'][] = $v;
    },$data);
    return $data['ret'];
}

This would return [2,1,3]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜