0

From an API request I receive JSON data. This is a dump of the array

Array(
    [0] => Array(
        [id] => 28257
        [price] => 18.50
        [meta_data] => Array(
            [0] => Array(
                [id] => 522793
                [key] => meta_image_out
                [value] => motor-watermerk.png
            )
            [1] => Array(
                [id] => 522794
                [key] => saveLoc
                [value] => Balie
            )
        )
    )
)

The data what I want is the value from

[1] => Array(
    [id] => 522794
    [key] => saveLoc
    [value] => Balie
)

for this I can use

echo $array[0]['meta_data'][1]['value'];

But the sequence from the array can be different and this code works not anymore.

$array[0]['meta_data'][**1**]['value'];

Number 1 can also 2, 3, 4...

How can I get always this value from the array?

The key is always saveLoc

ManojKiran A
  • 5,896
  • 4
  • 30
  • 43
Ben
  • 63
  • 1
  • 5
  • 1
    Does this answer your question? [PHP - find entry by object property from an array of objects](https://stackoverflow.com/questions/4742903/php-find-entry-by-object-property-from-an-array-of-objects). It's for arrays of objects, but the same principle can be applied to arrays of (associative) arrays, just by tweaking the syntax. – ADyson May 16 '20 at 18:33

1 Answers1

1

Try this. No need to worry about array index

    $data = [];
    foreach ($array as $arr) {
        $metaData = $arr['meta_data'];
        foreach ($metaData as $metaD) {
            if ($metaD['key'] == 'saveLoc') {
                $data = $metaD;
                break;
            }
        }
    }