PHP排序嵌套数组对象

时间:2018-06-24 14:19:29

标签: php arrays multidimensional-array

我有一个数组对象,里面有其他数组对象。某些数组的日期过长,例如

 [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 )

totals_grouped_by是一个子元素,因此:

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

返回day

我如何尝试每天退货,但我尝试了以下几种方法,但没有成功:

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

这给了我Invalid argument supplied for foreach()

我想获取每个日期的销售额,不确定如何访问它,因为它没有命名,而只是一个日期。希望有道理。

1 个答案:

答案 0 :(得分:1)

关于您的数据结构,有一些缺失的细节,我不确定您的数据可能如何变化,所以我将根据发布的零件来构造缺失的零件,并提供一些解决方案。

代码:(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";
}

输出:

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,
))