I have a PHP/MySQL e-commerce site that is placing order detail information into a serialized array alongside customer address info.
I want to be able to pull out the order items field, unserialize it and then combine the order items into one master array of ALL ordered items that can be manipulated in order to count how many orders of a specific product have been made.
The arrays look like this when I print_r the unserialized rows. Below are two order arrays, one with 3 products, and the second with only one.
The array values are ID #, SKU #, Quantity, Product Name, Price.
I want to be able to combine ALL orders into one array and then sum the total quantities for each unique ID or SKU number.
I realize this type of thing is drop dead simple if the data was clean in MySQL, but such is life. Any thoughts on how to manipulate these arrays would be truly appreciated.
In this case want to end up with 4 arrays, with the 629/01-3600 ones being combined such that the quantity value is now 2
Many thanks.
Array
(
[0] => Array
(
[1] => 488
[5] => 23-1000
[2] => 3
[3] => PRODUCT NAME
[4] => 2.50
)
[1] => Array
(
[1] => 423
[5] => 24-2300
[2] => 1
[3] => PRODUCT NAME
[4] => 3.50
)
[2] => Array
(
[1] => 506
[5] => 23-2800
[2] => 1
[3] => PRODUCT NAME
[4] => 2.50
)
[3] => Array
(
[1] => 629
[5] => 01-3600
[2] => 1
[3] => PRODUCT NAME
[4] => 7.50
)
)
Array
(
[0] => Array
(
[1] => 629
[5] => 01-3600
[2] => 1
[3] => PRODUCT NAME
[4] => 7.50
)
)
EDIT:
I wanted to add what eventually did what I was looking for
foreach($query->result as $row)
{
$items[] = unserialize( $row['FIELDNAME'] );
}
foreach($items as $item)
{
foreach($item as $order)
{
if( isset($output_array[$order[1]]) )
{
$output_array[$order[1]]['quantity'] += $order[2];
}
else
{
$output_array[$order[1]] = array (
'name' => $order[3],
'quantity' => $order[2],
'sku' => $order[5]
);
}
}
}
I then used this sorting function to sort on the quantity: http://www.php.net/manual/en/function.sort.php#99419