Unserialize from MySQL to a multidimensional PHP array

I have a PHP / MySQL e-commerce site that posts information about the details of an order in a serialized array along with information about the client address.

I want to be able to pull out the field of order elements, cancel it, and then combine the order elements into one main array of all ordered elements that can be manipulated to calculate the number of orders for a particular product.

Arrays look like this when I print unserialized strings. Arrays of two orders are presented below, one of which contains 3 products, and the second - only one.

Array values: ID #, SKU #, Quantity, Product Name, Price.

I want to be able to combine ALL orders into one array, and then summarize the total quantities for each unique identifier or SKU.

I understand that this type of thing is simply dead if the data was clean in MySQL, but this is life. Any thoughts on how to manipulate these arrays will really be appreciated.

In this case, you want the result to be 4 arrays, and the combinations 629 / 01-3600 were combined in such a way that now the quantity value is 2

Thank you very much.

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 that I 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] ); } } 

}

Then I used this sort function to sort by quantity: http://www.php.net/manual/en/function.sort.php#99419

+4
source share
1 answer

This is a bit of code, so I split it into pieces.

This is my recreation of your two arrays. The last line puts them in one.

 $items = array( array( 1 => 488, 5 => '23-1000', 2 => 3, 3 => 'PRODUCT NAME', 4 => 2.50 ), array( 1 => 423, 5 => '24-2300', 2 => 1, 3 => 'PRODUCT NAME', 4 => 3.50 ), array( 1 => 506, 5 => '23-2800', 2 => 1, 3 => 'PRODUCT NAME', 4 => 2.50 ), array( 1 => 629, 5 => '01-3600', 2 => 1, 3 => 'PRODUCT NAME', 4 => 7.50 ) ); $array_2 = array( array( 1 => 629, 5 => '01-3600', 2 => 1, 3 => 'PRODUCT NAME', 4 => 7.50 ) ); // Put the two arrays together (master array) $new_array = array_merge($items, $array_2); 

Then we create two arrays that will use SKU #, and the other for identifiers.

 // What the items will be sorted by $skus = array(); $ids = array(); 

The harder part

 // Loop through the combined items foreach( $new_array as $item ) { /** * Check if the item SKU number * has been added to the $skus array. * * If it has then add the old qty with the current item's * * Else, the item hasn't been added yet, * then add the entire item to the list */ if( isset($skus[$item[5]]) ) { // If it exists, then add the new qty $skus[$item[5]][2] += $item[2]; }else { // If it doesn't exist, then append it to the array $skus[$item[5]] = $item; } // Do the same thing as above // except for the id numbers if( isset($ids[$item[1]]) ) { // If it exists, then add the new qty $ids[$item[1]][2] += $item[2]; }else { // If it doesn't exist, then append it to the array $ids[$item[1]] = $item; } } 

Make sure everything is the way you want.

 echo '<h2>SKU Numbers</h2>'; echo '<pre>'; print_r($skus); echo '</pre>'; echo '<h2>ID Numbers</h2>'; echo '<pre>'; print_r($ids); echo '</pre>'; 

Hope this helps.

EDIT

For this code to work during the actual loop, you can try something like this. Although I'm not quite sure if this will work out of the box, this should be a good place to start.

 // What the items will be sorted by $skus = array(); $ids = array(); foreach( $result as $row ) { $row = unserialize($row); /** * MODIFIED * * Check if the $row SKU number * has been added to the $skus array. * * If it has then add the old qty with the current $row qty * * Else, the $row hasn't been added yet, * then add the entire $row to the list */ if( isset($skus[$row[5]]) ) { // If it exists, then add the new qty $skus[$row[5]][2] += $row[2]; }else { // If it doesn't exist, then append it to the array $skus[$row[5]] = $row; } // Do the same thing as above // except for the id numbers if( isset($ids[$row[1]]) ) { // If it exists, then add the new qty $ids[$row[1]][2] += $row[2]; }else { // If it doesn't exist, then append it to the array $ids[$row[1]] = $row; } } 
+2
source

All Articles