SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Enthusiast apsoda's Avatar
    Join Date
    Oct 2003
    Location
    UK
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation *Combining* duplicates in an array

    Hello,

    I have an array like this, created from a MySQL result set:

    PHP Code:
    $items[] = array(  
        
    "item_id" => $item["ID_ITEM"],
        
    "item_count" => $itemfinal["BUNDLE"],
        
    // other values here
    ); 
    There can be multiple rows with an identical $item_id and I'd like to create a new array with all the values intact, but with items of the same $item_id combined into one with a TOTAL of the $item_count.

    Despite my efforts to figure it out, I'm afraid I'm stumped as to how to do that.

    Thank you in advance for your help.

  2. #2
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    Have you got an example of how the final array should look?
    Salathe
    Software Developer and PHP Manual Author.

  3. #3
    SitePoint Enthusiast apsoda's Avatar
    Join Date
    Oct 2003
    Location
    UK
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, sorry..

    The original array would look like this:

    PHP Code:
    [0] => Array
            (
                [
    0] => Array
                    (
                        [
    item_id] => FIBC-0010-S-80X80X80
                        
    [item_bundle] => 0010
                        
    [item_description] => description
                        
    [item_price] => 19.00
                    
    )

                [
    1] => Array
                    (
                        [
    item_id] => FIBC-0010-S-80X80X80
                        
    [item_bundle] => 0010
                        
    [item_description] => description
                        
    [item_price] => 19.00
                    
    )

                [
    2] => Array
                    (
                        [
    item_id] => FIBC-0010-S-80X80X80
                        
    [item_bundle] => 0010
                        
    [item_description] => description
                        
    [item_price] => 19.00
                    
    )

                [
    3] => Array
                    (
                        [
    item_id] => FIBC-0010-S-90X90X90
                        
    [item_bundle] => 0010
                        
    [item_description] => description
                        
    [item_price] => 19.00
                    
    )

            ) 
    I'd like it to look like this:

    PHP Code:
    [0] => Array
            (
                [
    0] => Array
                    (
                        [
    item_id] => FIBC-0010-S-80X80X80
                        
    [item_bundle] => 0030
                        
    [item_description] => description
                        
    [item_price] => 19.00
                    
    )

                [
    1] => Array
                    (
                        [
    item_id] => FIBC-0010-S-90X90X90
                        
    [item_bundle] => 0010
                        
    [item_description] => description
                        
    [item_price] => 19.00
                    
    )

            ) 

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    This is easily accomplished with mySQL's GROUP BY clause and SUM function.

    change your query to SELECT ... SUM(item_bundle)... FROM ... GROUP BY item_id ...

  5. #5
    SitePoint Enthusiast apsoda's Avatar
    Join Date
    Oct 2003
    Location
    UK
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you, StarLion. However, I do need to keep the original array intact.

    I was assuming I'd need to create a new array, either by extracting the information I need from the original, or by pulling the data from the database again and cross referencing it against the original.

  6. #6
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by apsoda View Post
    I was assuming I'd need to create a new array, either by extracting the information I need from the original, or by pulling the data from the database again and cross referencing it against the original.
    If you want to do it in php then code like this should do it:
    PHP Code:
    $newArray = array();  // here we will accumulate grouped data

    foreach ($originalArray as $row) {  // loop through ungrouped data from db
      
    if (isset($newArray[$row['item_id']])) {
        
    // another occurence of this item_id
        
    $newArray[$row['item_id']]['item_count'] += $row['item_bundle'];
      } else {
        
    // first occurence of item_id
        
    $newArray[$row['item_id']] = array(
          
    'item_id' => $row['item_id'],
          
    'item_count' => $row['item_bundle'],
          
    'item_description' => $row['item_description'],
          
    'item_price' => $row['item_price'],
        );

    I assume that $originalArray contains elements of the top index 0 of your sample data, you will need to use $originalArray[0] with the structure you posted. The resulting $newArray will have item_id as keys, if you need a numerically indexed array just use array_values($newArray) at the end.

  7. #7
    SitePoint Enthusiast apsoda's Avatar
    Join Date
    Oct 2003
    Location
    UK
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have, in fact, been able to accomplish what I needed to by nesting MySQL queries based on StarLion's response.

    Thank you very much


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •