...

View Full Version : inserting values into an array from a foreach loop?



LJackson
09-03-2009, 02:38 PM
Hi All,

i have this loop

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

funnymoney
09-03-2009, 02:53 PM
maybe this..


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

LJackson
09-03-2009, 03:01 PM
hi mate this prints out


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

whizard
09-03-2009, 03:03 PM
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() (http://us3.php.net/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

LJackson
09-03-2009, 03:14 PM
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

whizard
09-03-2009, 03:18 PM
Can we see all the code

LJackson
09-03-2009, 03:29 PM
the solution funnymoney gave


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


returns this when using print_r


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

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

funnymoney
09-03-2009, 05:50 PM
it's a wild guess but what would this do




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


or perhaps this




$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



foreach($similar_products as $info)
{
print_r($info->ASIN);
exit;
}

LJackson
09-03-2009, 06:51 PM
ok i did a print_r

and it returned

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

LJackson
09-03-2009, 06:56 PM
ok this

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

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

prints out the same?

funnymoney
09-03-2009, 07:25 PM
have you tried with Simple XML (http://hr.php.net/manual/en/book.simplexml.php)


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


print_r($similar_products)

LJackson
09-03-2009, 09:19 PM
no mate havent tried the simple xml because it seems to working ok, just seems to hold too much data

here (http://www.a2sdeveloper.com/decode-a2s-query.html?url=http%3A%2F%2Fecs.amazonaws.com%2Fonca%2Fxml%3FService%3DAWSECommerceService%26AWSAcce ss%26Operation%3DItemSearch%26SearchIndex%3DBooks%26Author%3DRowling%26ResponseGroup%3DLarge) 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

Coyote6
09-03-2009, 09:54 PM
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.



// 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";



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum