Jump to content

array_combine query


shadiadiph

Recommended Posts

MM never had to use the array_combine functiion before but I can't seem to get it working its only returning the first two values in the table and an array with one value I would of thought that when its in the while loop it would return the lot??

 

function getcountrylist(){

$cc=array();
$cn=array();
$countrylist=array();

$countryquery = "SELECT * FROM `cc`";
$rcountryquery = mysql_query($countryquery);
while($row=mysql_fetch_array($rcountryquery)){

$cc= array($row["cc"]);
$cn= array($row["cn"]);

$countrylist= array_combine($cc,$cn);

return $countrylist;

}
//-----END while($row=mysql_fetch_array($rcountryquery))

}

Link to comment
https://forums.phpfreaks.com/topic/201699-array_combine-query/
Share on other sites

You have the return statement inside your loop, so it's doing one iteration.

 

I'm not sure what you want to return, but try this:

<?php
function getcountrylist(){
$countrylist=array();
$countryquery = "SELECT * FROM `cc`";
$rcountryquery = mysql_query($countryquery);
while($row=mysql_fetch_assoc($rcountryquery)){
    $countrylist[]= array($cc,$cn);
}
return $countrylist;
}
?>

 

Ken

 

Link to comment
https://forums.phpfreaks.com/topic/201699-array_combine-query/#findComment-1058060
Share on other sites

mm thanks that didn't work but put me on the right track kind of I now have

 

function getcountrylist(){

$cc=array();
$cn=array();
$countrylist=array();

$countrylist=array();
$countryquery = "SELECT * FROM `cc`";
$rcountryquery = mysql_query($countryquery);
while($row=mysql_fetch_assoc($rcountryquery)){    
$cc = array($row['cc']);
$cn = array($row['cn']);

$countrylist[]=array_combine($cc,$cn);
}
return $countrylist;
}

 

Only thing is it returns an array in an array

 

Array ( [0] => Array ( [AP] => Asia/Pacific Region ) [1] => Array ( [AU] => Australia ) [2] => Array ( [uS] => United States ) [3] => Array ( [CA] => Canada ) 

 

how do i get rid of Array ( [0] =>

Link to comment
https://forums.phpfreaks.com/topic/201699-array_combine-query/#findComment-1058063
Share on other sites

If you surround the array values with


tags when posting to the forum, they will look much better.

 

Try this code to get the array you want:

<?php
function getcountrylist(){
$countrylist=array();
$countryquery = "SELECT * FROM `cc`";
$rcountryquery = mysql_query($countryquery);
while($row=mysql_fetch_assoc($rcountryquery)){
    $countrylist[$row['cc']]= $row['cn'];
}
return $countrylist;
}
?>

 

Ken

 

Link to comment
https://forums.phpfreaks.com/topic/201699-array_combine-query/#findComment-1058072
Share on other sites

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.