Q:

Get array value for a specific key

I want to get the value of meta_value if the meta_key above equals first_name, how can I do that?
I was doing the following:
echo $name_data[1]['meta_value'];

This doesn't work as the meta_value isn't always in the [1] position.
This is the data, there's hundreds of these entries but the data is all mixed up (i.e the same data isn't always in the 0,1,2 etc arrays).
Array
(
    [0] => Array
        (
            [umeta_id] => 3221
            [user_id] => 130
            [meta_key] => nickname
            [meta_value] => email@domain.com
        )

    [1] => Array
        (
            [umeta_id] => 3222
            [user_id] => 130
            [meta_key] => first_name
            [meta_value] => Jim
        )

    [2] => Array
        (
            [umeta_id] => 3223
            [user_id] => 130
            [meta_key] => last_name
            [meta_value] => Jones
        )
)

Array
(
    [0] => Array
        (
            [umeta_id] => 3221
            [user_id] => 130
            [meta_key] => nickname
            [meta_value] => email@domain.com
        )

    [1] => Array
        (
            [umeta_id] => 3222
            [user_id] => 130
            [meta_key] => first_name
            [meta_value] => John
        )

    [2] => Array
        (
            [umeta_id] => 3223
            [user_id] => 130
            [meta_key] => last_name
            [meta_value] => Jones
        )
)

A:

You can use array_search with array_column to find the appropriate index to get the value from:
$k = array_search('first_name', array_column($array, 'meta_key'));
if ($k !== false) 
    $first_name = $array[$k]['meta_value'];

More generically, you can write a function to return the value for a given key:
function get_value($array, $key) {
    $k = array_search($key, array_column($array, 'meta_key'));
    if ($k !== false) 
        return $array[$k]['meta_value'];
    else
        return '';
}

echo get_value($array, 'first_name') . PHP_EOL;
echo get_value($array, 'nickname') . PHP_EOL;

Output:
Jim
email@domain.com

Demo on 3v4l.org

