Announcement

Collapse
No announcement yet.

Combining arrays?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Combining arrays?

    Hi,

    I have two arrays which are the result of 2 mysql queries. I want to combine them so I can and loop through them as I would a single returned result array.

    I've tried using array_merge, and $newarray = $array1 + $array2;, but neither of those methods seems to give me the right output.

    Any ideas on how I would do that? Current code pasted below, thanks:

    $test=mysql_fetch_array($result);
    $row2=mysql_fetch_array($result2);

    $row = array_merge($test, $row2);

    for($i=0,$n=10; $i<$n; $i++) {
    print $row[username];
    }

  • #2
    what's wrong? is it not merging the two arrays, or are you not printing the data correctly?

    Comment


    • #3
      Can't tell exactly, the output is totally wrong ... it prints out dozens of a single row (and no others), but I don't know if the problem is with the loop that prints it out, or that the arrays are not getting combined correctly.

      Comment


      • #4
        lol.... I just noticed something...

        your taking two record sets, and combining them...
        then your trying to loop through them like normal...

        look at your FOR loop...
        for($i=0,$n=10; $i<$n; $i++) {
        print $row[username];
        }

        where in there do you tell it to go to the next recordset? you don't... you don't use $i...

        try:
        for($i=0,$n=10; $i<$n; $i++) {
        print $row[$i];
        }

        or

        while ($row = mysql_fetch_array($result) { print $row[username]; }
        while ($row = mysql_fetch_array($result2) { print $row[username]; }

        Comment

        widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
        Working...
        X