I'm having trouble getting something to work.

  1. Is there an reason that array_push would not work with an associative array?

  2. Can you use variables (ie, a mysql_fetch_array array to populate an associative array?

~~

I'm trying to create a single array which consists of the results of two mysql queries; each of which returns two fields .These are ID and NAME, and ID and AKA, respectively, but I'm using AS in the query to change the AKA fieldname to NAME.

I have to combine these results into a single array and then sort them before working with the output.

OK. So I take the results of query1, use mysql-fetch-assoc and a while loop to--I think--convert this result into an associative array:

if (!$new) $new = array();
While ($Row) $new = array_Push($new, "$Row[name]" => "$Row[ID]");

Then I add the results of the second query:

While ($Row2) $new = array_Push($new, "$Row2[name]" => "$Row2[ID]");

I think what I'm doing is creating an empty array and then populating it one row at a time. Is that correct?

However, it's not working.

Can anyone offer suggestions?

    You're overwriting your array with each iteration. Plus you're using old, depreciated syntax. Here's how it should look...

    if(!isset($new)) {
       $new = array(); 
    }
    
    while($row = mysql_fetch_array($sql_results)) {
       $new[] = array($row['name'] => $row['ID']);
    } 

    Lots of things to look into here. [man]isset[/man] or [man]empty[/man] will check if a variable is set and replaces the !var syntax which throws an E_NOTICE error. It looked like you were setting mysql_fetch_array up as a variable, then saying while($row). Not needed. Can be done with one statement. Lastly, array_push is depreciated for the above syntax, which puts the values directly into the array.

      OK, something is different. With your changes incorporated I have

      if(!isset($new)) {
      $new = array();
      }

      while($row = mysql_fetch_array($result1)) {
      $new[] = array($row['name'] => $row['ID']);
      }
      while($row2 = mysql_fetch_array($result2)) {
      $new[] = array($row2['name'] => $row2['ID']);
      }

      ksort($new);

      This produced results (a step up!) but not good results, so I added:

      foreach ($new as $name => $group_tag) {
      print ("$name, $ID<br>");

      }
      To see what's in $new

      It results in:

      0, Array
      1, Array
      2, Array
      ...
      422, Array
      423, Array

      Which isn't what I was expecting. What's wrong?

      Edited to add:

      I tried:

      while($row = mysql_fetch_array($result1)) {
      print ("$row[name], $row[ID]<br>");
      $new[] = ..etc,

      and this printed good data followed by 400 lines of 0, Array

      I also tried commenting out the second While loop, and got the same result.

        Instead of doing the foreach, do a print_r($new). You'll see that the array goes $new[numeric key][name][id]. See if that comes back with what you want. If you don't want the numeric key, the do $new[$row['name']] = $row['ID'] instead of $new[] = array($row['name'] => $row['ID']), but if you have duplicate names, they'll overwrite each other.

          print_r produces:

          Array ( [0] => Array ( [GoodName] => GoodID\ )

          etc.

          How do I parse this array to produce the terms I want--name and ID?

          print ("$new[0][1], $new[0][2]<br>");

          produced: Array[1], Array[2]

            foreach($new as $a) {
               foreach($a as $k => $v) {
                  echo $k.', '.$v.'<br />';
               }
            }
              Write a Reply...