Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ignezio's avatar

Getting Data from Multi associative array

I am trying to get data from a multi associative json array and was wondering how i would access information such as 20-18-04-06 or the one following ive tried to foreach through the array but it only returns the first array given (array 4) while to arrays im trying to access is array 7

 array:2 [▼
"Meta Data" => array:4 [▼
"1. Information" => "Monthly Adjusted Prices and 
   Volumes"
"2. Symbol" => "AAPL"
"3. Last Refreshed" => "2018-04-06"
"4. Time Zone" => "US/Eastern"
  ]
"Monthly Adjusted Time Series" => array:219 [▼
"2018-04-06" => array:7 [▼
  "1. open" => "167.8800"
  "2. high" => "174.2304"
  "3. low" => "164.4700"
  "4. close" => "168.3800"
  "5. adjusted close" => "168.3800"
  "6. volume" => "164408813"
  "7. dividend amount" => "0.0000"
  ]
  "2018-03-29" => array:7 [▼
  "1. open" => "178.5400"
  "2. high" => "183.5000"
  "3. low" => "164.9400"
  "4. close" => "167.7800"
  "5. adjusted close" => "167.7800"
  "6. volume" => "701387082"
  "7. dividend amount" => "0.0000"
   ]

I've tried to reach the array it but it only returns the top results

 "Meta Data" => array:4 [▼
"1. Information" => "Monthly Adjusted Prices and 
   Volumes"
"2. Symbol" => "AAPL"
"3. Last Refreshed" => "2018-04-06"
"4. Time Zone" => "US/Eastern"
 ]

my control is below any help is greatly appreciated.

    $client = new Client(); //GuzzleHttp\Client
    $promise = $client->get('https');
    $body = $promise->getbody();
    $result = json_decode($body,true);
     foreach ($result as $key => $value) {
         return view('pages.test', ['re' => $value]);
   }
0 likes
12 replies
Nash's avatar
<!-- Don't do this -->
foreach ($result as $key => $value) {
    return view('pages.test', ['re' => $value]);
}

The return statement immediately ends the execution of a function. Your view will be loaded with only the first subarray passed to it. You need to loop through them in your view instead:

@foreach ($result as $key => $level2_array)
    ...
 
    @foreach ($level2_array as $key2 => $value2)
        ...
    @endforeach
@endforeach

Controller:

return view('pages.test', ['result' => $result]);

Edit: you can also use Laravel's array_get helper to retrieve a specific value from a nested array https://laravel.com/docs/master/helpers#method-array-get

ignezio's avatar

I used this to try to access the data

{{$value2['1. open']}}

and got this error any idea I tried composer update

Snapey's avatar
foreach($result['Monthly Adjusted Time Series'] as $record) {

    dump($record);
}
ignezio's avatar

@Snapey how would i call a specific key would i do {{dump($record['1. open']) ?

edit. nevermind i got it.

Cronix's avatar

@ignezio It looks like it. Does it not work (besides missing the }} at the end)?

ignezio's avatar

@Cronix yes it works i had forgotten to put the }} at the end of my comment.

@Snapey is there a way i could do the foreach in my controller? whenever i try i only get back one result if i where to do $record['1. open'] i only get back the first key. is there a way i could get back every key without having to put the foreach in my view?

Cronix's avatar

You'd have to get them individually if you don't want to use a loop

{{ $result['Meta Data']['1. Information'] }}
{{ $result['Meta Data']['2. Symbol'] }}
// ...

{{ $result['Monthly Adjusted Time Series']['2018-04-06']['1. open'] }}
{{ $result['Monthly Adjusted Time Series']['2018-04-06']['2. high'] }}
// ...

{{ $result['Monthly Adjusted Time Series']['2018-03-29']['1. open'] }}
{{ $result['Monthly Adjusted Time Series']['2018-03-29']['3. low'] }}
// etc

If this is not what you're needing, describe a little better what you're wanting to do:

is there a way i could get back every key without having to put the foreach in my view

This doesn't make much sense to me. What data are you wanting to get to use in the view? I don't see why it matters whether you do the loop in the controller or the view...it's still a loop.

ignezio's avatar

@cronix sorry for the vague question, i would like to take every "1. open" from the array and store them in a variable so i can pass them to a chart then display the chart in the view. i understand i would have to do something like

$data =  $result['Monthly Adjusted Time Series']['2018-04-06']['2. high'];

but the problem with using it that way is that the months will change that's why i wanted to use a foreach in the controller so i could do something like

$result->['1. open'] and it will cycle through the array without having to name each key individually

ignezio's avatar

when ever i try to loop in the controller i am only able to pull the first result while in the view i am able to pull multiple values.

$client = new Client(); //GuzzleHttp\Client
$promise = $client->get('https');
$body = $promise->getbody();
$result = json_decode($body,true);
    foreach ($result['Monthly Adjusted Time Series'] as $record) {
      $data = $record['1. open'];
    }
    return view('pages.test', ['data' => $data]);
}
Cronix's avatar
$data = [];
foreach ($result['Monthly Adjusted Time Series'] as $record) {
      $data[] = $record['1. open'];
}

like that? $data is now an array of 2 values from "1. open" from both date arrays.

@foreach ($data as $value)
    <div>{{ $value }}</div>
@endforeach

//or if you don't want to loop over them

{{ $data[0] }}

{{ $data[1] }}

Do you need the date as well? If so change it to $data[$record] = $record['1. open']; Now its an array with 2 values, with the date being the key.

@foreach ($data as $date => $value)
    <div>{{ $date }} = {{ $value }}</div>
@endforeach
Snapey's avatar

If you put return inside a loop then it will only run once. Simple fact. It does not matter what you are looping over.

Please or to participate in this conversation.