$row){ if(strpos(strtolower($row[$qtype]),$query) === false){ unset($rows[$key]); } } } //Make PHP handle the sorting $sortArray = array(); foreach($rows AS $key => $row){ $sortArray[$key] = $row[$sortname]; } $sortMethod = SORT_ASC; if($sortorder == 'desc'){ $sortMethod = SORT_DESC; } array_multisort($sortArray, $sortMethod, $rows); $total = count($rows); $rows = array_slice($rows,($page-1)*$rp,$rp); } header("Content-type: application/json"); $jsonData = array('page'=>$page,'total'=>$total,'rows'=>array()); foreach($rows AS $row){ //If cell's elements have named keys, they must match column names //Only cell's with named keys and matching columns are order independent. $entry = array('id'=>$row['iso'], 'cell'=>array( 'name'=>$row['name'], 'iso'=>$row['iso'], 'printable_name'=>$row['printable_name'], 'iso3'=>$row['iso3'], 'numcode'=>$row['numcode'] ), ); $jsonData['rows'][] = $entry; } echo json_encode($jsonData);