13

I want to create an array of associative arrays in a while loop. In each iteration of the while loop I want to add a new element in the array. How I can do that? After that I want to pass this array in a foreach and print the data. I have this part of code for now but obviously something is wrong with that.

while($row2 = mysql_fetch_array($result))
{ 
    $myarray = array("id"=>$theid, "name"=>name($id), "text"=>$row2['text']);
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
anna
  • 745
  • 2
  • 9
  • 30
  • I have two arrays and i want to put them in one variable. I am using this line of code $finalarray = $_SESSION['SESS_ARRAY'] + $myarray;. But it returns to me a fatal error. Can you help me? – anna Apr 18 '12 at 22:35
  • I've solved it using the array_merge(). – anna Apr 18 '12 at 22:39

4 Answers4

25

To add an element in the end of an array use []
Example:

$myarray[] = array("id"=>$theid, "name"=>name($id), "text"=>$row2[text]);
laltin
  • 1,134
  • 12
  • 20
8

Obviously, okay, first pick it apart so there's something to learn:

while($row2 = mysql_fetch_array($result))
{
    ...
}

This part look's okay, let's look inside the loop:

$myarray = array("id"=>$theid, "name"=>name($id), "text"=>$row2[text]);

There are multiple points. Probably most important is, as that is inside a loop, you overwrite $myarray in each iteration. You want to add to an array instead. Let's do this:

$myarray = array(); # initialize the array first!
while($row2 = mysql_fetch_array($result))
{
    $myarray[] = $row2; # add the row
}

After that you can output it to proof that it basically works:

var_dump($myarray);

That shows you an array that contains all rows. You then only need to change your database query so that it only returns the fields you're interested in.

In case you can't do that with the database, you can manipulate the array as well:

$myarray = array(); # initialize the array first!
while($row2 = mysql_fetch_array($result))
{
    $myarray[] = array(
        "id"   => $theid, 
        "name" => name($id), 
        "text" => $row2['text']
    );
}
var_dump($myarray);

Now the result should look like you want it. To output $myarray:

foreach ($myarray as $number => $row)
{
    echo '<div>Number ', $number, ':<dl>';
    foreach ($row as $k => $v)
    {
        printf("<dt>%s</dt><dd>%s</dd>\n", $k, htmlspecialchars($v));
    }
    echo '</dl></div>'
}
hakre
  • 193,403
  • 52
  • 435
  • 836
4

If you're trying to add to $myarray in each iteration, do it like this:

$myarray[] = array("id"=>$theid, "name"=>name($id), "text"=>$row2[text]);

or like this:

array_push($myarray, array("id"=>$theid, "name"=>name($id), "text"=>$row2[text]));
lafor
  • 12,472
  • 4
  • 32
  • 35
2

Obviously your access to $row2 looked wrong, so I assumed that here to be right

$myarray = array();
while($row2 = mysql_fetch_array($result)) { 
  // append something to your array with square brackets []
  $myarray[] = array("id"=> $row2['id'], "name" => $row2['name'], "text"=>$row2['text']);


  // or to maker this even shorter you could do
  $myarray[] = $row2; // ... because it has the same array key names
}

Then later when you want to read from it:

foreach($myarray as $val) {
  echo $val['name'].' (ID: '.$val['id'].') wrote following text: '.$val['text'];
}
dan-lee
  • 14,365
  • 5
  • 52
  • 77