Building array to pass to json_encode

I have been really stuck on figuring out how to build an array for json_encode.

I am looping through an array of column names, querying the database for distinct values and then trying to load those distinct values into an array to pass to json_encode. I can’t see to get the correct format, so the json is not being formatted correctly.

I need this format,

[["1001","1002","1003"],["Lura Baggs","Dominga Conary","Randolph Kiester"]]

But I can’t seem to get it. Here is my code,

[php]
$sTable = “People”;
$aColumns = array( ‘FName’, ‘LName’ );

for ( $i=0 ; $i<count($aColumns) ; $i++ )
{

//echo($i);
$sQuery = "
SELECT DISTINCT “.$aColumns[$i].”
FROM $sTable
";

$rSelectList = mysql_query( $sQuery, $gaSql[‘link’] ) or die(mysql_error());

$j = 0;
$vSelectValue = $vSelectValue."[";
while($row = mysql_fetch_array($rSelectList)){
$aSelectList[i][j] = $row;
$j++;
}
[/php]

Which should, in my limited knowledge of PHP, add the value of the $row to an array, then I can json_encode the array so that it will be formatted like the example above.

What am I missing here?

Thanks in advance,
Drew

Been playing with it for a while and finally got it. Figured I’d post back to show the code,

[php]
$sTable = “People”;
$aColumns = array( ‘FName’, ‘LName’ );

for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
//echo($i);
$sQuery = "
SELECT DISTINCT “.$aColumns[$i].”
FROM $sTable
";

$rSelectList = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());


$row = array();
while ( $aRow = mysql_fetch_array( $rSelectList ) )
{
	//$row = array();
	$row[] = $aRow[ $aColumns[$i] ];
}
$output['select'][] = $row;

}
[/php]

This code generates this when run through json_encode(),

["1001","1002","1003"]["Selma Aquirre","Minerva Hepner","Harvey Yeadon"]

I hope this helps someone else,
Drew

Forgive me for posting such unclean code, this code has the debugging comments stripped out, and one debug comment added for testing…

[php]
$sTable = “People”;
$aColumns = array( ‘FName’, ‘LName’ );
//select lists
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
$sQuery = "
SELECT DISTINCT “.$aColumns[$i].”
FROM $sTable
";

$rSelectList = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());

$row = array();
while ( $aRow = mysql_fetch_array( $rSelectList ) )
{
	$row[] = $aRow[ $aColumns[$i] ];
}
    //echo json_encode($row);
$output['select'][] = $row;

}
[/php]

Drew

Sponsor our Newsletter | Privacy Policy | Terms of Service