您好我正在做一些操作,我需要从它的键中获取数组的值。如何获得基于键匹配的数组值
我有$attr_color
变量值为red
。
因此,如果red
在数组中,那么它需要返回它的值。
下面是我的数组:
Array
(
[0] => Array
(
[label] =>
[value] =>
)
[1] => Array
(
[label] => red
[value] => 32
)
[2] => Array
(
[label] => green
[value] => 33
)
[3] => Array
(
[label] => pink
[value] => 34
)
[4] => Array
(
[label] => black
[value] => 35
)
[5] => Array
(
[label] => white
[value] => 36
)
)
我曾尝试下面的代码,但它返回空白:
$attr_color = "red";
//$response is my array which i have mention above.
if(in_array($attr_color,array_column($response,"label")))
{
$value = $response['value'];
echo "Value".$value;
exit;
}
帮助?我犯了什么错误?
你无法直接访问$ response ['value']。这就是你在做错什么 –
你必须用'label = red'获得数组的索引,然后使用'$ response [$ index] ['value']' –