PHP:迭代对象数组

时间:2016-09-17 08:42:39

标签: php arrays object

我从最近30分钟开始对这段代码感到头疼。

$orderData = $orderData->get();
//var_dump($orderData);exit;
$orderFinal = array();
foreach ($orderData as $order) {
    //var_dump($order->id);
    if(in_array($order->id, $orderFinal)){
        $orderFinal[$order->id] = (array) $order;
    }else{
        $orderFinal[$order->id] = (array) $order;
    }
    // var_dump($orderFinal[$order->id]);
}
var_dump($orderFinal);exit;

$ OrderData看起来像这样 - >

array(4) {
  [0]=>
  object(stdClass)#299 (7) {
    ["id"]=>
    int(1)
    ["created_at"]=>
    string(19) "2016-09-16 12:07:18"
    ["status"]=>
    int(0)
    ["part_name"]=>
    string(14) "some part name"
    ["ordered_by"]=>
    int(1)
    ["quantity"]=>
    int(11)
    ["order_title"]=>
    string(10) "Some title"
  }
  [1]=>
  object(stdClass)#300 (7) {
    ["id"]=>
    int(32)
    ["created_at"]=>
    string(19) "2016-09-16 10:03:50"
    ["status"]=>
    int(0)
    ["part_name"]=>
    string(16) "new machine part"
    ["ordered_by"]=>
    int(1)
    ["quantity"]=>
    int(12)
    ["order_title"]=>
    string(9) "asdasdasd"
  }
  [2]=>
  object(stdClass)#301 (7) {
    ["id"]=>
    int(35)
    ["created_at"]=>
    string(19) "2016-09-16 10:07:17"
    ["status"]=>
    int(0)
    ["part_name"]=>
    string(28) "another awesome machine part"
    ["ordered_by"]=>
    int(1)
    ["quantity"]=>
    int(123)
    ["order_title"]=>
    string(15) "Some Order Name"
  }
  [3]=>
  object(stdClass)#302 (7) {
    ["id"]=>
    int(35)
    ["created_at"]=>
    string(19) "2016-09-16 10:07:17"
    ["status"]=>
    int(0)
    ["part_name"]=>
    string(14) "some part name"
    ["ordered_by"]=>
    int(1)
    ["quantity"]=>
    int(1022)
    ["order_title"]=>
    string(15) "Some Order Name"
  }
}  

我想要这样的结果:

array(3) {
  [1]=>
  array(7) {
    ["id"]=>
    int(1)
    ["created_at"]=>
    string(19) "2016-09-16 12:07:18"
    ["status"]=>
    int(0)
    ["part_name"]=>
    string(14) "some part name"
    ["ordered_by"]=>
    int(1)
    ["quantity"]=>
    int(11)
    ["order_title"]=>
    string(10) "Some title"
  }
  [32]=>
  array(7) {
    ["id"]=>
    int(32)
    ["created_at"]=>
    string(19) "2016-09-16 10:03:50"
    ["status"]=>
    int(0)
    ["part_name"]=>
    string(16) "new machine part"
    ["ordered_by"]=>
    int(1)
    ["quantity"]=>
    int(12)
    ["order_title"]=>
    string(9) "asdasdasd"
  }
  [35]=>
        [0]=>array(7) {
        ["id"]=>
        int(35)
        ["created_at"]=>
        string(19) "2016-09-16 10:07:17"
        ["status"]=>
        int(0)
        ["part_name"]=>
        string(28) "another awesome machine part"
        ["ordered_by"]=>
        int(1)
        ["quantity"]=>
        int(123)
        ["order_title"]=>
        string(15) "Some Order Name"
        },
  [1]=>array(7) {
    ["id"]=>
    int(35)
    ["created_at"]=>
    string(19) "2016-09-16 10:07:17"
    ["status"]=>
    int(0)
    ["part_name"]=>
    string(14) "some part name"
    ["ordered_by"]=>
    int(1)
    ["quantity"]=>
    int(1022)
    ["order_title"]=>
    string(15) "Some Order Name"
  }
}

我得到的是 -

array(3) {
  [1]=>
  array(7) {
    ["id"]=>
    int(1)
    ["created_at"]=>
    string(19) "2016-09-16 12:07:18"
    ["status"]=>
    int(0)
    ["part_name"]=>
    string(14) "some part name"
    ["ordered_by"]=>
    int(1)
    ["quantity"]=>
    int(11)
    ["order_title"]=>
    string(10) "Some title"
  }
  [32]=>
  array(7) {
    ["id"]=>
    int(32)
    ["created_at"]=>
    string(19) "2016-09-16 10:03:50"
    ["status"]=>
    int(0)
    ["part_name"]=>
    string(16) "new machine part"
    ["ordered_by"]=>
    int(1)
    ["quantity"]=>
    int(12)
    ["order_title"]=>
    string(9) "asdasdasd"
  }
  [35]=>
  array(7) {
    ["id"]=>
    int(35)
    ["created_at"]=>
    string(19) "2016-09-16 10:07:17"
    ["status"]=>
    int(0)
    ["part_name"]=>
    string(14) "some part name"
    ["ordered_by"]=>
    int(1)
    ["quantity"]=>
    int(1022)
    ["order_title"]=>
    string(15) "Some Order Name"
  }
}

任何帮助都将受到高度赞赏。 感谢

2 个答案:

答案 0 :(得分:1)

尝试以下代码:

 $orderData = $orderData->get();
 $orderFinal = array();
 foreach ($orderData as $order) {
    //var_dump($order->id);
     if(array_key_exists($order->id, $orderFinal)){

        if (! isset($orderFinal[$order->id][0] ) ){
            $orderFinal[$order->id][0] = $orderFinal[$order->id];
        }
        $orderFinal[$order->id][] = (array) $order;

    }else{
        $orderFinal[$order->id] = (array) $order;
    }
    // var_dump($orderFinal[$order->id]);
}
var_dump($orderFinal);exit;

答案 1 :(得分:0)

你的if语句的两个分支都做同样的事情,所以显然有些事情是不对的。这应该可以解决问题:

$orderFinal = array();
foreach ($orderData as $order) {
    if (in_array($order->id, array_keys($orderFinal))) {
        if (isset($orderFinal[$order->id]["id"])) {
            $orderFinal[$order->id] = array($orderFinal[$order->id], (array) $order);
        } else {
            $orderFinal[$order->id][] = (array) $order;
        }
    } else {
        $orderFinal[$order->id] = (array) $order;
    }
}