Reputation: 2362
I have been scratching my head over this code from last 30 min.
$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 looks like this->
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"
}
}
I want result like this:
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"
}
}
What I am getting is -
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"
}
}
Any help will be highly appreciated. Thanks
Upvotes: 1
Views: 40
Reputation: 570
Try below code:
$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;
Upvotes: 1
Reputation: 5359
Both branches of your if statement do the same thing, so obviously something's not right. This should do the trick:
$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;
}
}
Upvotes: 0