Jump to content

A simple question : How do i store them in array during loop?


niceduncan

Recommended Posts

In the following code to retrive data from MySQL:

 

while ($menu_row = mysqli_fetch_array($result, MYSQLI_NUM)) {

echo "<option value=\"$menu_row[0]\">$menu_row[1]</option>\n";

}

I want to store sets of $menu_row[0] => $menu_row[1] in a new array for later use.

How do I achieve that? For example:

 

while(){

$new_array[] = $menu_row[0] => $menu_row[1];

}

 

but it wont work!!!

Thanks a lot

A few things to note about arrays:

 

1. You can dynamically extend array sizes, eg:

<?php

 

$array = array();

while($row = mysql_fetch_assoc($resultset)){

  $array[] = $row[0];

}

 

?>

 

2. Arrays can be dynamically called:

<?php

 

$array = array("number1","number2","number3");

for($i=0;$i<count($array);$i++){

  echo($array[$i]);

  echo("<br>");

}

?>

 

3. Arrays can be as many dimensions as needed:

<?php

 

$array = array(

array(

array(

array(

array(

"1" => array(

array(

"a")

)

),

array(

"2" => "b"

)

)

)

)

);

?>

 

No. 2 is probably what your after.

 

-CB-

A few things to note about arrays:

 

1. You can dynamically extend array sizes, eg:

<?php

 

$array = array();

while($row = mysql_fetch_assoc($resultset)){

  $array[] = $row[0];

}

 

?>

 

2. Arrays can be dynamically called:

<?php

 

$array = array("number1","number2","number3");

for($i=0;$i<count($array);$i++){

  echo($array[$i]);

  echo("<br>");

}

?>

 

3. Arrays can be as many dimensions as needed:

<?php

 

$array = array(

array(

array(

array(

array(

"1" => array(

array(

"a")

)

),

array(

"2" => "b"

)

)

)

)

);

?>

 

No. 2 is probably what your after.

 

-CB-

 

Thanks for trying to help me. No.2 is not the solution but all three of them gives me the hint ^_^

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.