2016-09-17 167 views
1

我一直在撓撓我的頭從這最後30分鐘的代碼。PHP:迭代對象數組

$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" 
    } 
} 

任何幫助將受到高度讚賞。 謝謝

回答

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; 
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; 
    } 
}