I've been trying to create a search for my website using Sphinx. I query multiple indexes since the type of data is totally different in each, but I need to sort all of that data by weight and display it.
Here's the foreach loop that I use. All of the commented out code is things that I've already tried.
function addToSrchArray($res, $restype)
{
global $total_result_count, $new_name_array;
$resultCount = $res['total_found'];
if ($resultCount > 0) {
$total_result_count = $total_result_count + $resultCount;
$srchtp = $restype;
foreach ($res["matches"] as $key => $val) {
$arrToAdd = array(
'indx' => $key,
'wgt' => $val["weight"],
'typ' => $srchtp
);
if ($val["weight"] > $new_name_array[0]["wgt"]) {
//add to begining
array_unshift($new_name_array, $arrToAdd);
} else {
//echo count($new_name_array);
$i = 0;
while($i < count($new_name_array)) {
// echo $i.' '.$srchtp.'<br>';
//for($i = 0; $i < count($new_name_array); $i++) {
if($val["weight"] > $new_name_array[$i]["wgt"]) {
//array_splice($new_name_array, $i, 0, array('indx' => $key, 'wgt' => $val["weight"], 'typ' => $srchtp));
array_unshift(array_slice($new_name_array, 0, $i), array_unshift($arrToAdd, array_slice($new_name_array, $i)));
/*array_slice($new_name_array, 0, $i) +
$arrToAdd +
array_slice($new_name_array, $i, count($new_name_array)-1);*/
//$new_name_array[] = array('indx' => $key, 'wgt' => $val["weight"], 'typ' => $srchtp);
break;
} else if ($i == (count($new_name_array) - 1)) {
$new_name_array[] = array(
'indx' => $key,
'wgt' => $val["weight"],
'typ' => $srchtp
);
break;
}
$i++;
}
}
}
//var_dump($new_name_array);
}
}
Each of these different methods have different success levels. There are 45 results, some of these methods show 9, some show 22, etc. The $res parameter passed to the function looks something like this
["matches"] => array(2) {
[3]=> array(2) {
["weight"]=> string(1) "3"
["attrs"]=> array(1) {
["follow_count"]=> string(1) "1"
}
}
[2]=> array(2) {
["weight"]=> string(1) "2"
["attrs"]=> array(1) {
["follow_count"]=> string(1) "1"
}
}
}
["total"]=> string(1) "2"
["total_found"]=> string(1) "2"
["time"]=> string(5) "0.000"
Please help. I've tried everything. Nothing shows me all of the results sorted by weight.