Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts

    Unhappy inserting values into an array from a foreach loop?

    Hi All,

    i have this loop
    PHP Code:
    foreach($similar_products as $info)
    {
        print 
    $info->ASIN;

    which gives me a result like
    B002AQQVDCB001U3ZUKGB0027UY818B001GNBUWWB001WAK91W

    there are 5 different codes in there, and i need to somehow insert each one into an array so that they appear in the array in the same order as printed out.

    but im not sure how to insert the values into an array?

    the array name is $similar_items

    can anyone please help
    thanks

  • #2
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    maybe this..

    PHP Code:
    foreach($similar_products as $info)
    {
     
    $array[] = $info->ASIN;
    }
    print_r($array); 

  • Users who have thanked funnymoney for this post:

    LJackson (09-03-2009)

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    hi mate this prints out

    Code:
    Array ( [0] => SimpleXMLElement Object ( [0] => B002AQQVDC ) [1] => SimpleXMLElement Object ( [0] => B001U3ZUKG ) [2] => SimpleXMLElement Object ( [0] => B0027UY818 ) [3] => SimpleXMLElement Object ( [0] => B001GNBUWW ) [4] => SimpleXMLElement Object ( [0] => B001WAK91W ) )
    does that look right to you? i dont know what im talking about but i'd say there is too much info in there?

    but if i print out $array[3] i get the expected result so maybe it is correct?

    thanks

  • #4
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    You have two options:

    You can either, as funnymoney said, use this syntax to fill an array:

    $myarray[] = "value"

    which will add value to the end of the array.

    Also, you can use the array_push() function.

    You should use the first syntax if you are only putting on value at a time into the array as it is more efficient, but you can use array_push() to put multiple values in simultaneously.

    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    hi dan thanks for your reply,

    yeah just inserting one value at a time

    it all seems to be working so im more than happy with funnymoney's solution, just thought the inclusion of:
    [0] => SimpleXMLElement Object (

    before each item in the array.

    cheers
    Luke

  • #6
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Can we see all the code
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #7
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    the solution funnymoney gave
    Code:
    foreach($similar_products as $info)
    {
     $similar_items[] = $info->ASIN;
    }
    returns this when using print_r

    Code:
    Array ( [0] => SimpleXMLElement Object ( [0] => B002AQQVDC ) [1] => SimpleXMLElement Object ( [0] => B001U3ZUKG ) [2] => SimpleXMLElement Object ( [0] => B0027UY818 ) [3] => SimpleXMLElement Object ( [0] => B001GNBUWW ) [4] => SimpleXMLElement Object ( [0] => B001WAK91W ) )
    where as i'd of expected this
    Code:
    Array ( [0] => B002AQQVDC  [1] => B001U3ZUKG [2] => B0027UY818 [3] => B001GNBUWW [4] => B001WAK91W )
    but like i said above it is working just looks different to what i'd expect

    thanks
    Luke

  • #8
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    it's a wild guess but what would this do


    PHP Code:
    foreach($similar_products as $info)
    {
     
    $similar_items[] = $info->ASIN['SimpleXMLElement Object'][0];

    or perhaps this

    PHP Code:

    $i
    =0;
    foreach(
    $similar_products as $info)
    {
     
    $similar_items[] = $info->ASIN[$i]['SimpleXMLElement Object'][0];
    $i++;

    problem is that your $info->ASIN should return just string you are looking for, and it doesn't so you need to find a way to grab it.

    can you do print_r on this

    PHP Code:
    foreach($similar_products as $info)
    {
     
    print_r($info->ASIN);
    exit;

    Last edited by funnymoney; 09-03-2009 at 05:57 PM.

  • #9
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    ok i did a print_r

    and it returned
    Code:
    SimpleXMLElement Object ( [0] => B002AQQVDC ) SimpleXMLElement Object ( [0] => B001U3ZUKG ) SimpleXMLElement Object ( [0] => B0027UY818 ) SimpleXMLElement Object ( [0] => B001GNBUWW ) SimpleXMLElement Object ( [0] => B001WAK91W )
    which again is odd,
    i'll try your other suggestions

    thanks
    Luke

  • #10
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    ok this
    Code:
    foreach($similar_products as $info)
    {
     $similar_items[] = $info->ASIN[$i]['SimpleXMLElement Object'][0];
    $i++;
    }  
    print_r($similar_items);
    prints out
    Array ( [0] => [1] => [2] => [3] => [4] => )

    and this
    Code:
    foreach($similar_products as $info)
    {
     $similar_items[] = $info->ASIN['SimpleXMLElement Object'][0];
    }
    prints out the same?

  • #11
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    have you tried with Simple XML

    PHP Code:
    foreach($similar_products as $info)
    {
     
    $similar_items[] =  simplexml_load_string($info->ASIN);

    }
    print_r($similar_items);
    var_dump($similar_items); 
    but again, this is blind guessing. if you can show your ASIN construct, or the whole class things could be much simpler

    and do this

    PHP Code:
    print_r($similar_products
    Last edited by funnymoney; 09-03-2009 at 07:28 PM.

  • #12
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    no mate havent tried the simple xml because it seems to working ok, just seems to hold too much data

    here is a link to a xml file markup similar to what im using

    this gets the ASIN code for my current code from the SimilarProducts node.

    cheers
    Luke

  • #13
    Regular Coder
    Join Date
    May 2009
    Location
    Moore, OK
    Posts
    282
    Thanks
    11
    Thanked 41 Times in 41 Posts
    When I need to write a list of values I normally just loop through the statement again with an echo statement and return just the values. The code below is just to show some other options that are possible.

    PHP Code:
    // You can also split you data into different arrays at the same time.
    $array1 = array ();
    $array2 = array ();

    foreach(
    $similar_products as $info) {
      
    $array1[] = $info->ASIN;
      
    $array2[] = $info->SOMEOTHERNAME;
    }

    // When ever you are ready to write these into you page... start an unordered list or what ever you want.
    echo "<h3>ASIN</h3>\n<ul>\n";
    foreach (
    $array1 as $a) {
      echo 
    "  <li>$a</li>\n";
    }
    // Finish the list.
    echo "</ul>\n";

    // Then if you want to include the key you can do this too.
    echo "<h3>SOMEOTHERNAME</h3>\n<ul>\n";
    foreach (
    $array2 as $key=>$value) {
      echo 
    "  <li>$key=>$value</li>\n";
    }
    // Finish the list.
    echo "</ul>\n"


  •  

    Posting Permissions

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