2

I am using an api to show a list of times but struggling to display them in using foreach

Here is how the data is shown:

stdClass Object
(
    [id] => 2507525
    [snapshotTimes] => Array
        (
            [0] => 2020-10-02T04:04:41+00:00
            [1] => 2020-10-03T03:22:29+00:00
            [2] => 2020-10-04T03:06:43+00:00
            [3] => 2020-10-04T21:18:11+00:00
            [4] => 2020-10-06T03:07:12+00:00
            [5] => 2020-10-07T03:21:31+00:00
            [6] => 2020-10-10T03:43:00+00:00
            [7] => 2020-10-17T02:58:49+00:00
            [8] => 2020-10-19T02:57:35+00:00
            [9] => 2020-10-23T03:08:28+00:00
            [10] => 2020-10-26T04:02:51+00:00
            [11] => 2020-10-27T04:33:19+00:00
        )

)

Code:

$domainArray = $services_api->getWithFields("/package/2507525/web/timelineBackup/web");
foreach ($domainArray as $arr) {
    $Time = $arr->$domainArray->snapshotTimes;
    echo " TIME: $Time<br>";
}

But it doesn't seem to echo anything at all? Where am I going wrong?

Designer
  • 477
  • 2
  • 12
  • 2
    `snapshotTimes` is an array, but you are treating it as if it was a string. you should probably run another inner `foreach` to cycle through all values within `snapshotTimes` . Check your PHP Error log. – Martin Oct 27 '20 at 20:23
  • 1
    Perhaps an example would help him @Martin? – Vogal Oct 27 '20 at 20:25
  • 2
    @Vogal oh go on then, if you insist..... see my example below – Martin Oct 27 '20 at 20:31

3 Answers3

2

Your code shows;

$Time = $arr->$domainArray->snapshotTimes;

Here you're tying to access a property called domainArray on the array given by the foreach(). No need to do that since your already using the foreach() to loop over the data;

$domainArray = $services_api->getWithFields("/package/2507525/web/timelineBackup/web");

// For each item in the 'snapshotTimes' array
foreach ($domainArray->snapshotTimes ?? [] as $time) {
    echo " TIME: {$time}<br>";
}

Try it online!


Note: Using the null coalescing operator (?? []) to ensure snapshotTimes exists in the data.


Based on comments; the same solution but with array_reverse() to reverse the output.

foreach (array_reverse($domainArray->snapshotTimes) as $time) {
    ....

Try it online!

0stone0
  • 34,288
  • 4
  • 39
  • 64
1

You are triying to print snapshotTimes but you created a loop for another thing. If you want to print snapshotTimes code will be like :

foreach($arr->$domainArray->snapshotTimes as $time){
    echo $time."</br>";
}
Filayer
  • 51
  • 4
1

snapshotTimes is an array, but you are treating it as if it was a string. you should probably run another inner foreach to cycle through all values within snapshotTimes . Check your PHP Error log.

Perhaps an example would help him @Martin?

Example:

$domainArray = $services_api->getWithFields("/package/2507525/web/timelineBackup/web");
foreach ($domainArray as $arr) {
   if(is_array($arr->snapshotTimes) && count($arr->snapshotTimes) > 0 ){
    $times = $arr->snapshotTimes;
    foreach($times as $timeRow){
        echo " TIME: ".$timeRow."<br>";
    }
    unset($times); //tidy up temp vars.
    }
}

I underline the point that you need to check your PHP Error Log to help you diagnose these sort of structure issues.

Notes:

  • Your reference $arr->$domainArray->snapshotTimes within the foreach is incorrect, you're referencing both the foreach label as well as the source of the foreach label, which will result in an error.
  • PHP variables should start with a lower case letter.
  • If you don't need $domainArray => $arr for any other reason within the foreach loop, you can simplify the loop by looping the array rather than the container, as 0stone0 shows on their answer.
Martin
  • 22,212
  • 11
  • 70
  • 132