SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding Array to Array

    Hi all, I have a shopping cart for a mobile repair company, when a user adds something to their basket, the basic details of the phone are added, then the actual repair (such as a screen replacement, new battery, etc) are added under the options element of the array. How can I add to the options array when a customer adds another repair to their basket for the same phone? Hope I've explained well enough...

    PHP Code:
    $item = array(
                
    'id' => $product->repair_id,
                
    'qty' => 1,
                
    'price' => 0.00,
                
    'name' => $product->name.' '.$product->repair_name,
                
    'img' => $product->repair_image,
                
    'link' => 'fix/'.$product->url,
                
    'options' => array('price' => 5.60'name' => 'Screen'), array('price' => 5.60'name' => 'New Battery')
            ); 

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,237
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $item['options'][] = array('price' => 5.60'name' => 'New Battery'); 

  3. #3
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, awesome thank you so much! Goodness knows why I didn't use this, brain freeze!

  4. #4
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    67 Post(s)
    Tagged
    2 Thread(s)
    Taken from http://us3.php.net/array_push

    bart at framers dot nl 26-Sep-2001 10:16 Array_push also works fine with multidimensional arrays. Just make sure the element is defined as an array first.

    <?php
    $array["element"][$element]["element"] = array();
    array_push ($array["element"][$element]["element"], "banana");
    ?>



  5. #5
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    67 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by cpradio View Post
    PHP Code:
    $item['options'][] = array('price' => 5.60'name' => 'New Battery'); 
    Won't this overwrite all of options?

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by K. Wolfe View Post
    Won't this overwrite all of options?
    No. It adds to a new entry to the array. "[]" acts like auto-increment ID in MySQL.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,237
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    No. It adds to a new entry to the array. "[]" acts like auto-increment ID in MySQL.
    Correct, though I like to think of the [] invoking array_push automatically for you

  8. #8
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by coxdabd View Post
    PHP Code:
    $item = array(
                
    'id' => $product->repair_id,
                
    'qty' => 1,
                
    'price' => 0.00,
                
    'name' => $product->name.' '.$product->repair_name,
                
    'img' => $product->repair_image,
                
    'link' => 'fix/'.$product->url,
                
    'options' => array('price' => 5.60'name' => 'Screen'), array('price' => 5.60'name' => 'New Battery')
            ); 
    Side note: What you've defined there is not a multidimensional array. I -think- you did this just as an example, but what you meant was an array of arrays...

    PHP Code:
                'options' => array(array('price' => 5.60'name' => 'Screen'), array('price' => 5.60'name' => 'New Battery')) 
    The initial code would have looked like:
    PHP Code:
    $item = array(
                
    'id' => $product->repair_id,
                
    'qty' => 1,
                
    'price' => 0.00,
                
    'name' => $product->name.' '.$product->repair_name,
                
    'img' => $product->repair_image,
                
    'link' => 'fix/'.$product->url,
                
    'options' => array('price' => 5.60'name' => 'Screen'), 
                
    => array('price' => 5.60'name' => 'New Battery')
            ); 
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


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
  •