Reputation: 385
I have an array looks like this.I want to merge array by their order id.
Array(
[0] => Array
(
[orderId] => 152
[prodName] => Red Dri-fit Undershirt
[quantity] => 2
[cartId] => 677
)
[1] => Array
(
[orderId] => 151
[prodName] => Practice Shorts
[quantity] => 2
[cartId] => 667
)
[2] => Array
(
[orderId] => 151
[prodName] => Red Dri-fit Undershirt
[quantity] => 2
[cartId] => 668
)
)
It should look something like this.
Array
(
[152] => Array
(
[prodName] => Red Dri-fit Undershirt
[quantity] => 2
[cartId] => 677
)
[151] => Array
(
[1] => Array
(
[prodName] => Practice Shorts
[quantity] => 2
[cartId] => 667
)
[2] => Array
(
[prodName] => Red Dri-fit Undershirt
[quantity] => 2
[cartId] => 668
)
)
)
I am trying two these two arrays by their order ids. I am looking for a function, if any, to preserve or merge values into similar key, but no luck so far.
Upvotes: 1
Views: 2098
Reputation: 768
I think this is much more straight forward.
$collections = array();
foreach($products as $product){
if(!empty($collections[$product['orderId']]) || !isset($collections[$product['orderId']])){
array_push($collections[$product['orderId']],
array(
'prodName' => $product['prodName'],
'quantity' => $product['quantity'],
'cartId' => $product['cartId'],
'isPack' => $product['isPack']
)
);
}else{
$collections[$product['orderId']] = array(
array(
'prodName' => $product['prodName'],
'quantity' => $product['quantity'],
'cartId' => $product['cartId'],
'isPack' => $product['isPack']
)
);
}
}
echo '<pre>';
print_r($collections);
Upvotes: 1
Reputation: 34
$order_key_id=array();
foreach($my_array as $key => &$value)
{
$orderid=$value->get_orderID();
$order_key_id[$key][]=$value;
}
Upvotes: 0
Reputation: 30
$orders = array(
array (
'orderId' => 152,
'prodName' => 'Red Dri-fit Undershirt'
),
array (
'orderId' => 151,
'prodName' => 'Red Dri-fit Undershirt'
),
array (
'orderId' => 151,
'prodName' => 'Red Dri-fit Undershirt'
)
);
$orders_byid = array();
foreach($orders as $v){
$order = $orders_byid[$v['orderId'];
if ($order){
if(!is_array($order[0])){
unset($orders_byid[$v['orderId']);
$orders_byid[$v['orderId'][] = $order;
}
$orders_byid[$v['orderId'][] = $v;
} else {
$orders_byid[$v['orderId'] = $v;
}
}
Upvotes: 0
Reputation: 971
With php 5, whip up a quick class to model it, then use asort to sort them and, if you want, move them into another array. Like this:
class MyThing
{
private orderID;
private prodName;
private quantity;
private cartID;
private isPack;
public get_OrderID() {return $this->orderID;}
public load_values_from_array(array &$data)
{
$this->orderID=$data['orderId'];
$this->prodName=$data['prodName'];
/* ... go on ...*/
}
public static order(MyThing &$a, MyThing &$b)
{
if($a->orderID > $b->orderID) return 1;
else if($a->orderID < $b->orderID) return -1;
else return 0;
}
}
Create the array, fill it up with your classes using "load_values_from_array" and order it like this:
uasort($my_array, array('MyThing', 'order'));
If you want to have the key values as the orderID you can do this:
$order_key_id=array();
foreach($my_array as $key => &$value)
{
$orderid=$value->get_orderID();
if(!isset($order_key_id[$orderid])) $order_key_id[$orderid]=array();
$order_key_id[$orderid][]=$value;
}
Of course, you can skip the class part and go nuts on it using code that looks and behaves like the last bit, like this:
$order_key_id=array();
foreach($my_array as $key => &$value)
{
$orderid=$value['orderID'];
if(!isset($order_key_id[$orderid])) $order_key_id[$orderid]=array();
unset($value['orderID'];
$order_key_id[$orderid][]=$value;
}
I did not test the code, but I think you'll catch the drift.
Upvotes: 0