简体   繁体   中英

Get first parent key of value from Multidimensional Array in PHP

How to get the first parent key of value from Multidimensional Array in PHP

Sample Code :

$arr = array(
    "a" => array("1"=>"!","2"=>"@","3"=>"#"),
    "b" => array("4"=>"$","5"=>"%","6"=>"^"),
    "c" => array("7"=>"&","8"=>"*","9"=>"(")
);

echo array_search("%",$arr);

Require Output: b

You can filter the array returning only the array(s) that contain what you are searching for and get the key:

echo key(array_filter($arr, function($v) { return array_search("%", $v); }));

Obviously if it is found in more than one array you will have to decide which key you want. The above will give you the first key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM