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"
}
}
任何幫助將受到高度讚賞。 謝謝