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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.