Page 1 of 1

PHP - Array_merge help...

Posted: Sun Sep 27, 2009 4:53 pm
by flapjack
Hello,

I need some advice. I wrote this query which pulls values in from my database. It then merges the two arrays so that the key is the id number and the value is the name.

When I output it using a for each loop, the key is still starting at 0 instead of starting using the key that I set.

Can anyone see what I am doing wrong?

The Code:

Code: Select all

 
$query = "SELECT name FROM campaign"; 
$result = mysql_query($query) or die(mysql_error());
 
    if ($row = mysql_fetch_array($result))
                {                 
                    $idarray = $row["id"];
                    $namearray = $row["name"];
                }
 
            while ($row = mysql_fetch_array($result))
                {
                    $idarray .= "||" . $row["id"];
                    $namearray .= "||" . $row["name"];
                }
        
                   $arrayid =  explode("||" , $subjectarray);   
                   $arrayname = explode("||" , $namearray); 
                  
//merge the aray so that the key = id and the value = name
$campaign_array = array_merge($arrayid,$arrayname);
The output:

Code: Select all

<?php
                                //delete the naturally created 0 value in the array
                                unset($campaign_array[0]);
                                
                                //echo out in table format
                                foreach($campaign_array as $key => $value){
                                
                                echo "<tr height='30' onmouseover=style.backgroundColor='#DBE6F9' onmouseout=style.backgroundColor='white'>
                                <td><a href='campaign.php?name=".$key."&action=view'><font color=#CC0000>".$key."</font></a></td>
                                <td><a href='campaign.php?name=".$key."&action=view'><font color=#CC0000>".$value."</font></a></td>";
                                $query = "SELECT * FROM subscribers WHERE `campaign`='$key' AND `status`='1'"; 
                                $result = mysql_query($query) or die(mysql_error());
                                $numrows = mysql_num_rows($result);
                                echo "<td>$numrows</td>
                                <td><a href='campaign.php?name=".$key."&action=view'><font color=#CC0000>[edit]</font></a></td>
                                </tr>";
                                
                                }
                            
                                ?>
 
The result:

I deleted ID 1 so it should start with the number 2. But the results start wit hthe correct name, not the correct ID.

Re: PHP - Array_merge help...

Posted: Sun Sep 27, 2009 5:02 pm
by requinix
I think you're confusing array_merge with array_combine...

Re: PHP - Array_merge help...

Posted: Tue Sep 29, 2009 11:08 pm
by flapjack
Thanks,

Your absolutely right! Thats what you get for programming into the early hours of the next day!