I have an object which contains keys with values that are objects, and inside of those objects there are another objects as value for one of the keys. I have been able to get a specific value from the deep nested object I need by doing 4 loops, but I was wondering if there is a better way of doing so. Here is the object I have:
{
record1: {
key1: value1,
key2: value2,
key3: {
key_x1: value_x1,
key_x2: value_x2,
key_x3: value_x3
}
}
record2: {
key1: value1,
key2: value2,
key3: {
key_x1: value_x1,
key_x2: value_x2,
key_x3: value_x3
}
}
}
So, for each record (record1
and record2
etc.) I need to pull out the value from key_x1
and store it in an array. Any ideas on how to accomplish this without having 4 foreach loops?
EDIT: Here is what I have which works, just looks ugly:
$mainObject
$a = [];
$dateArray = [];
foreach ($mainObject as $individualOb) {
$dates = $individualOb->getDate();
$a[] = $dates;
}
if (empty($a)) {
return true;
} else {
foreach ($a as $date) {
foreach ($date as $key => $value) {
if ($key == 'date') {
$dateArray[] = $value;
}
}
}
foreach ($dateArray as $value) {
if(strtotime($value) < strtotime('-1 year') ) {
return true;
}
}
}