Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3315064
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:18:22+00:00 2026-05-17T22:18:22+00:00

I have a PHP/MySQL e-commerce site that is placing order detail information into a

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:18:23+00:00Added an answer on May 17, 2026 at 10:18 pm

    This is a little bit of code so I’ve separated it into chunks.

    This is my recreation of your two arrays. The last line puts them into 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);
    

    Next, we create two arrays to that will use the SKU# and another for the IDs.

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

    The more complicated part

    // Loop through the combined items
    foreach( $new_array as $item ) {
    
      /**
       * Check if the item's 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 it

    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. While I’m not completely sure if this will work out of the box, it 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's SKU number
        * has been added to the $skus array.
        * 
        * If it has then add the old qty with the current $row's 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;
      }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP/MySQL site I'm thinking about converting into a HTML5/JavaScript that could
This is for MySQL and PHP I have a table that contains the following
I have a php page that displays rows from a mysql db as a
I have a php / mysql / jquery web app that makes 13 ajax
We have a PHP/MYSQL application that collects user input, including special characters like ø,ü,ñ,
I have a PHP website backed by a MySQL database and a small team
I have a PHP web application which uses a MySQL database for object tagging,
Using PHP and MySQL, I have a forum system I'm trying to build. What
We have got loads of options for php + MySQL + Apache combo... Which
I'm writng a small application in PHP + MySQL and have come to the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.