PHP sort nested array object

Abu Nooh Source

I have an array object which has other array object inside it. Some of the arrays are dated e.g.

 [totals_grouped_by] => day [totals] => stdClass Object ( 
[2018-05-25] => stdClass Object ( [sales] => 0.00 [orders] => 0 [items] => 0 [tax] => 0.00 [shipping] => 0.00 [discount] => 0.00 [customers] => 0 ) 
[2018-05-26] => stdClass Object ( [sales] => 0.00 [orders] => 0 [items] => 0 [tax] => 0.00 [shipping] => 0.00 [discount] => 0.00 [customers] => 0 ) 
[2018-05-27] => stdClass Object ( [sales] => 0.00 [orders] => 0 [items] => 0 [tax] => 0.00 [shipping] => 0.00 [discount] => 0.00 [customers] => 0 )

The totals_grouped_by is an child element, so:

foreach($results as $val){
    echo $val->totals_grouped_by;}

returns day

How can I return sales for each day, I have tried below and a few other possibilities without success:

foreach($results as $val){
    foreach($val->totals_grouped_by as $day){
       echo $day->sales."<br>";}
       }

This gives me Invalid argument supplied for foreach()

I would like to get sales for each date, not sure how to access it as it is not named but it just a date. Hope that makes sense.

phparraysmultidimensional-array

Answers

answered 3 months ago mickmackusa #1

There a couple of missing details about your data structure and I am not sure how your data may vay, so I'll fabricate the missing parts based on the posted parts and offer a couple of solutions.

Code: (Demo)

$results = (object)[
    (object)[
        "totals_grouped_by" => "day",
        "totals" => (object)[
            "2018-05-25" => (object)[
                "sales" => "0.00", "orders" => 0, "items" => 0, "tax" => "0.00", "shipping" => "0.00", "discount" => "0.00", "customers" => 0
                ],
            "2018-05-26" => (object)[
                "sales" => "0.00", "orders" => 0, "items" => 0, "tax" => "0.00", "shipping" => "0.00", "discount" => "0.00", "customers" => 0
                ],
            "2018-05-27" => (object)[
                "sales" => "0.00", "orders" => 0, "items" => 0, "tax" => "0.00", "shipping" => "0.00", "discount" => "0.00", "customers" => 0
                ]
            ]
        ]
    ];

foreach($results as $val) {
    echo "Proof of mostly-accurate multi-dimensional array structure: " , $val->totals_grouped_by;
}

echo "\n---\n";
// Loop the first level, then loop the totals level
foreach ($results as $val) {
    foreach ($val->totals as $date => $subarray) {
        echo "Showing $date: ";
        var_export($subarray);
        echo "\n";
    }
}

// or assuming all levels are objects and you only want the one "totals" object that you posted:
echo "\n---\n";

foreach ($results->{0}->totals as $date => $subarray) {
    echo "Showing $date: ";
    var_export($subarray);
    echo "\n";
}

Outputs:

Proof of mostly-accurate multi-dimensional array structure: day
---
Showing 2018-05-25: stdClass::__set_state(array(
   'sales' => '0.00',
   'orders' => 0,
   'items' => 0,
   'tax' => '0.00',
   'shipping' => '0.00',
   'discount' => '0.00',
   'customers' => 0,
))
Showing 2018-05-26: stdClass::__set_state(array(
   'sales' => '0.00',
   'orders' => 0,
   'items' => 0,
   'tax' => '0.00',
   'shipping' => '0.00',
   'discount' => '0.00',
   'customers' => 0,
))
Showing 2018-05-27: stdClass::__set_state(array(
   'sales' => '0.00',
   'orders' => 0,
   'items' => 0,
   'tax' => '0.00',
   'shipping' => '0.00',
   'discount' => '0.00',
   'customers' => 0,
))

---
Showing 2018-05-25: stdClass::__set_state(array(
   'sales' => '0.00',
   'orders' => 0,
   'items' => 0,
   'tax' => '0.00',
   'shipping' => '0.00',
   'discount' => '0.00',
   'customers' => 0,
))
Showing 2018-05-26: stdClass::__set_state(array(
   'sales' => '0.00',
   'orders' => 0,
   'items' => 0,
   'tax' => '0.00',
   'shipping' => '0.00',
   'discount' => '0.00',
   'customers' => 0,
))
Showing 2018-05-27: stdClass::__set_state(array(
   'sales' => '0.00',
   'orders' => 0,
   'items' => 0,
   'tax' => '0.00',
   'shipping' => '0.00',
   'discount' => '0.00',
   'customers' => 0,
))

comments powered by Disqus