0

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.

3
  • Which format are you using in the first code? Can you write it in print_r or var_export format? Commented Jun 24, 2018 at 15:02
  • @Barun It's the above std object. Commented Jun 24, 2018 at 15:12
  • The OP has posted print_r() data. This is apparent from the bracketed keys, missing commas, and unquoted values. In the future, please post your data using var_export() because it is easier for volunteers to prepare your data for our coding attempts. Commented Jun 24, 2018 at 15:20

1 Answer 1

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,
))
Sign up to request clarification or add additional context in comments.

2 Comments

This is fine, I'll have to update my code as I have left some bits out. I need to access the date and echo it too.
I am echoing the date before the var_export() call to show you how to do it. If you don't want to simply dump the subarray data as I have done, you can write another foreach() like this: foreach($subarray as $key => $value) { echo "$key : $value";}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.