简体   繁体   中英

How to get value of array based on matching on key

Hi i am working on some operations where i need to get value of array from its key.

I have $attr_color variable with the value red .

So if red is in the array then it needs to be return its value.

Below is my array :

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
        )

)

I have tried below code but it returns blank :

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

Help ? where i made mistake ?

Try this simple solution, hope this will help you out. Here we are using array_column for getting columns and indexing it with keys and values , Where keys are labels and values as value

Try this code snippet (with sample inputs)

$result=array_column($array, 'value',"label");
$result=array_filter($result);
echo $result["red"];

In your case it's enough to use a regular foreach loop:

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

foreach ($response as $item) {
    if ($item['label'] == $attr_color) {
        $value = $item['value'];
        break;   // avoids redundant iterations
    }
}

Use array_search , and check for false :

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

By using array_column with third parameter and array_search as

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

Try below code : using array match function :

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

Use array_search instead of in_array

$attr_color = "red";

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

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

Try:

$attr_color = "red";

//$response is my array which i have mention above.

$index = array_search($attr_color, array_column($response, 'label'));

if($index!==false){
    $value = $response[$index]['value'];
    echo "Value:".$value;
    exit;
}

Here $index will get the index of the array with label red

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