0

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
        )
)
Rob
  • 6,304
  • 24
  • 83
  • 189
  • 4
    Loop and check if `meta_key` is `first_name`, then get the value? Most obvious option I can think of. [array_search](http://php.net/array_search) with [array_column](http://php.net/array_column) may also be an option. – Jonnix Sep 03 '19 at 08:29
  • 1
    You could use foreach() to iterate through the object until you find a defined meta_value. There are functions in PHP that return the lower(first) element and upper(last) element of an array. – SPlatten Sep 03 '19 at 08:29
  • usually you use a `foreach` and an `if` with these sort of things. and ideally a `break` once the search is over – Kevin Sep 03 '19 at 08:34
  • Do you retrieve other information as well or just the `first_name`? – Nigel Ren Sep 03 '19 at 08:36

2 Answers2

1

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

Nick
  • 138,499
  • 22
  • 57
  • 95
0

With foreach,

foreach($name_data as $meta_data){
    if("first_name" == $meta_data["meta_key"]){
        echo $meta_data["meta_value"];
    }
}
LF00
  • 27,015
  • 29
  • 156
  • 295