Php 如何基于关键字匹配获取数组值

Php 如何基于关键字匹配获取数组值,php,arrays,Php,Arrays,嗨,我正在做一些操作,我需要从它的键中获取数组的值 我有$attr\u color变量,值为红色 因此,如果red在数组中,则需要返回其值 下面是我的数组: Array ( [0] => Array ( [label] => [value] => ) [1] => Array ( [label] => red

嗨,我正在做一些操作,我需要从它的键中获取数组的值

我有
$attr\u color
变量,值为
红色

因此,如果
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
        )

)
$your_value = array_search($attr_color, array_column($response,"label"));
if ($index !== false) {
    echo $response[$your_value]['value'];
}
我尝试了以下代码,但返回为空:

$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;
}

帮忙?我哪里出错了?

使用
array\u search
而不是在数组中使用

$attr_color = "red";

if(($index = array_search($attr_color,array_column($response,"label")))!==FALSE)
{

    $value = $response[$index]['value'];
    echo "Value".$value;
    exit;
}
尝试:


这里$index将获得标签为红色的数组的索引

试试这个简单的解决方案,希望这能帮到你。这里我们使用
array\u column
获取列,并使用
键和
值对其进行索引,其中
键和
值是
标签和

(带样本输入)


在您的情况下,使用常规的
foreach
循环就足够了:

$attr_color = "red";
$value = "";

foreach ($response as $item) {
    if ($item['label'] == $attr_color) {
        $value = $item['value'];
        break;   // avoids redundant iterations
    }
}
使用,并检查是否为false

$index = array_search($attr_color, array_column($response,"label"));
if ($index !== false) {
    echo $response[$index]['value'];
}

使用带有第三个参数的
array\u列
,并将
array\u搜索
作为

 $attr_color="red";
 $arr = array_filter(array_column($response, "label", 'value'));// pass thired parameter to make its key
    if (array_search($attr_color, $arr)) {// use array search here

        echo array_search($attr_color, $arr);
    }

尝试以下代码:使用数组匹配函数:

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
        )

)
$your_value = array_search($attr_color, array_column($response,"label"));
if ($index !== false) {
    echo $response[$your_value]['value'];
}

您不能直接访问$response['value']。这就是您所做的错误。您必须使用
label=red
获取数组的索引,然后使用
$response[$index]['value']
当颜色出现在数组的第一个元素
array\u search
返回匹配的索引时,将找不到该颜色,如果数组中没有相应的颜色,那么它将返回false。我在这里搜索标签数组,而不是原始数组。这是最短的解决方案,而不是使用foreach循环。但我尊重你的所有回答。你试过我在第一次评论中指出的吗?你得到输出了吗?好的,我现在知道了。零索引将返回false。无论如何,谢谢。当颜色出现在阵列的第一个元素中时,它不会找到颜色。实际上,这对我来说是可行的解决方案。谢谢