Sam Sam - 1 month ago 15
PHP Question

PHP - Dealing with a Indexed Arrays

I have an array of arrays that I'm finding trouble looping with foreach and getting accessing the data.

They array key has dates, then the array inside this has badly ordered indexes.

array(
[2016-11-03T07:00:00] => Array
(
[0] => stdClass Object
(
[Job] => stdClass Object
(
[ID] => 87126387136
[Foo] => stdClass Object
(
[ID] => 0
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)

)

[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)

[Premises] => stdClass Object
(

[Location] => stdClass Object
(

[Metric] => stdClass Object
(
[Latitude] => 51.3453534534
[Longitude] => -0.417308
)

)

[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)

)

[ScheduledStart] => 2016-11-03T07:00:00
)

)

[6] => stdClass Object
(
[Job] => stdClass Object
(
[ID] => 234923749
[Foo] => stdClass Object
(
[ID] => 0
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)

)

[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)

[Premises] => stdClass Object
(

[Location] => stdClass Object
(

[Metric] => stdClass Object
(
[Latitude] => 51.34535435345
[Longitude] => -0.417308
)

)

[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)

)

[ScheduledStart] => 2016-11-03T07:00:00
)

)

)

[2016-11-24T07:00:00] => Array
(
[3] => stdClass Object
(
[Job] => stdClass Object
(
[ID] => 3749823749
[Foo] => stdClass Object
(
[ID] => 0
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)

)

[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)

[Premises] => stdClass Object
(

[Location] => stdClass Object
(

[Metric] => stdClass Object
(
[Latitude] => 51.345345435
[Longitude] => -0.417308
)

)

[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)

)

[ScheduledStart] => 2016-11-24T07:00:00
)

)

[5] => stdClass Object
(
[Job] => stdClass Object
(
[ID] => 4792874923874
[Foo] => stdClass Object
(
[ID] => 0
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)

)

[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)

[Premises] => stdClass Object
(

[Location] => stdClass Object
(

[Metric] => stdClass Object
(
[Latitude] => 51.3453453444
[Longitude] => -0.417308
)

)

[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)

)

[ScheduledStart] => 2016-11-24T07:00:00
)

)

[9] => stdClass Object
(
[Job] => stdClass Object
(
[ID] => 9943386
[Foo] => stdClass Object
(
[ID] => 0
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)

)

[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)

[Premises] => stdClass Object
(

[Location] => stdClass Object
(

[Metric] => stdClass Object
(
[Latitude] => 51.34534534534
[Longitude] => -0.417308
)

)

[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)

)

[ScheduledStart] => 2016-11-24T07:00:00
)

)

)
)


Is it possible to loop through this array using foreach in it's current state?

I'm open to suggestion of manipulating the array if for a more efficient solution.

Answer

There are two layers of arrays - the first with dates as indices, the second with numbers as indices. Values of the second arrays are nested objects.

foreach ($arr as $date => $v) {
  echo "date: ".$date."<br>";
  foreach ($v as $number => $job) {
    echo "number: ".$number."<br>";
    echo $job->ID."<br>";
    echo $job->Foo->ID."<br>";
    echo $job->Foo->RecordStamp->AddedBy."<br>";
    echo $job->Premises->Location->Metric->Latitude."<br>";
    echo $job->ScheduledStart."<br>";
  }
}