简体   繁体   English

从 PHP 中的多维数组获取值的第一个父键

[英]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如何从 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需要输出: 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.以上将为您提供第一把钥匙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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