l!m!t Posted June 30, 2010 Share Posted June 30, 2010 Hello, I want to pull some fields into an array from a while loop, so I can later use them in a for loop. I am confused how to get the array from the while into the correct formatting. I have a mySQL while that grabs the data while ($postage =db_fetch_array($postage_query)){ $i++; $custname =$postage['delivery_name'] . '&'; $custaddress = $postage['delivery_street_address']; $buildArray = array("name_" . $i => $custname, "full_address_" . $i => $custaddress); $i; } ^ For obvious reasons the buildArray keeps looping the ""array"" how can I have the array only loop once like below? If I break it out it then fails to count $i... I want the formatting something like the below example. / Array ( [name_1] => John Doe => [name_2] => John Doe 2 => [full_address_1] => 1234 test street => [full_address_2] => 1234 test street 2; ) */ I then want to be able to loop in into a for each foreach ($buildArray as $key => $val){ echo $key . '' . $val; } Any help would be great I am still learning PHP arrays.. Link to comment https://forums.phpfreaks.com/topic/206239-php-array-from-while/ Share on other sites More sharing options...
travo1992 Posted June 30, 2010 Share Posted June 30, 2010 Im assuming you mean Array ( [name_1] => John Doe, [name_2] => John Doe 2, [full_address_1] => 1234 test street, [full_address_2] => 1234 test street 2 ) At the moment you are replacing the array every time you go through the while loop. Try $i=0; $buildArray = array(); while ($postage =db_fetch_array($postage_query)){ $i++; $custname =$postage['delivery_name'] . '&'; $custaddress = $postage['delivery_street_address']; $buildArray["name_" . $i] = $custname; $buildArray["full_address_" . $i] = $custaddress; } Link to comment https://forums.phpfreaks.com/topic/206239-php-array-from-while/#findComment-1078971 Share on other sites More sharing options...
l!m!t Posted June 30, 2010 Author Share Posted June 30, 2010 Thanks travo1992! That worked, much appreciated! Link to comment https://forums.phpfreaks.com/topic/206239-php-array-from-while/#findComment-1078976 Share on other sites More sharing options...
travo1992 Posted June 30, 2010 Share Posted June 30, 2010 No problems Link to comment https://forums.phpfreaks.com/topic/206239-php-array-from-while/#findComment-1079029 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.