您好我正在做一些操作,我需要從它的鍵中獲取數組的值。如何獲得基於鍵匹配的數組值
我有$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']' –