Jump to content

PHP array from while


l!m!t

Recommended Posts

 

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

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;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.