Php 按添加的变量对数组排序

Php 按添加的变量对数组排序,php,mysql,arrays,sorting,Php,Mysql,Arrays,Sorting,将变量添加到mysql查询的末尾。 现在我需要能够根据关联的值进行排序。有人能帮忙吗?我试过几种不同的排序方法 $result2 = mysqli_query($datacon, "SELECT * FROM spreadsheet WHERE brand = '$brand' AND package = '$package' AND HIDE_FROM_SEARCH = '0' GROUP BY A

将变量添加到mysql查询的末尾。 现在我需要能够根据关联的值进行排序。有人能帮忙吗?我试过几种不同的排序方法

$result2 = mysqli_query($datacon,
    "SELECT * FROM spreadsheet 
        WHERE brand = '$brand' 
            AND package = '$package' 
            AND HIDE_FROM_SEARCH = '0' 
        GROUP BY ACCTNAME $pages->limit");

echo mysqli_error($datacon);
while($row = mysqli_fetch_array($result2)) {
    $lat_B=$row['LATITUDE'];
    $long_B=$row['LONGITUDE'];

    $lat_A = round($lat_A,2);
    $long_A = round($long_A,2);
    $lat_B = round($lat_B,2);
    $long_B = round($long_B,2);
    $distance = new calcDist($lat_A, $long_A, $lat_B, $long_B);
    $distance = $distance->calcDist($lat_A, $long_A, $lat_B, $long_B);
    $distance = round($distance,2);
    $locationSort=array($row);
    $locationSort['distance']=$distance;

    FOR EACH SOMETHING?
}
我已经修改了我以前的帖子,以反映我对答案的使用

$result2 = mysqli_query($datacon,"SELECT * FROM spreadsheet WHERE brand = '$brand'
                AND package = '$package' and HIDE_FROM_SEARCH = '0' GROUP BY ACCTNAME $pages->limit");

                echo(mysqli_error($datacon));
                class sortBydistance
                {
                    function sortBydistance($a, $b) {
                        if ($a['distance'] == $b['distance']) {
                            return 0;
                        }
                        return ($a['distance'] < $b['distance']) ? -1 : 1;
                    }
                }
                while($row = mysqli_fetch_array($result2))
                {
                        $lat_B=$row['LATITUDE'];
                        $long_B=$row['LONGITUDE'];

                    $lat_A = round($lat_A,2);
                    $long_A = round($long_A,2);
                    $lat_B = round($lat_B,2);
                    $long_B = round($long_B,2);
                    $distance = new calcDist($lat_A, $long_A, $lat_B, $long_B);
                    $distance = $distance->calcDist($lat_A, $long_A, $lat_B, $long_B);
                    $distance = round($distance,2);


                }
                $locationSort=array($row);
                $locationSort['distance']=$distance;
                uasort($locationSort, array($this, "sortBydistance"));
                print_r($locationSort);
$result2=mysqli\u查询($datacon,“从电子表格中选择*,其中brand='$brand'
和package='$package'和HIDE_FROM_SEARCH='0'按ACCTNAME$pages->limit分组);
echo(mysqli_错误($datacon));
等级排序距离
{
函数排序距离($a,$b){
如果($a['distance']=$b['distance']){
返回0;
}
回报($a['distance']<$b['distance'])?-1:1;
}
}
while($row=mysqli\u fetch\u数组($result2))
{
$lat_B=$row[‘纬度’];
$long_B=$row[‘经度’];
$lat_A=圆形($lat_A,2);
$long_A=圆形($long_A,2);
$lat_B=圆形($lat_B,2);
$long_B=圆形($long_B,2);
$distance=新计算列表($lat_A、$long_A、$lat_B、$long_B);
$distance=$distance->calcDist($lat_A、$long_A、$lat_B、$long_B);
$distance=圆形($distance,2);
}
$locationSort=数组($row);
$locationSort['distance']=$distance;
uasort($locationSort,array($this,“sortBydistance”);
打印(位置排序);

可能您没有正确检索$bran和$package变量

$result2 = mysqli_query($datacon,
    "SELECT * FROM spreadsheet 
        WHERE brand = '".$brand."' 
            AND package = '".$package."' 
            AND HIDE_FROM_SEARCH = '0' 
        GROUP BY ACCTNAME ".$pages->limit.");

首先,创建条目数组,并将其存储在数组中,我们将调用
$rows

您想在这里使用PHP函数
usort
来提供自定义排序回调

假设您有一个如下所示的数组:

$rows = array(
    array("latitude" => 5, "distance" => 10),
    array("latitude" => 10, "distance" => 5),
    array("latitude" => 56, "distance" => 48)
);
您希望对该
$行数组
进行重新排序,使
纬度==10
的条目位于第一位,
纬度==5
位于第二位,
纬度==56
位于第三位,这取决于它们之间的距离

usort($rows, function ($a, $b) {
    $a = $a['distance'];
    $b = $b['distance'];

    return $a == $b ? 0 : $a > $b ? 1 : -1;
});
输出

Array
(
    [0] => Array
        (
            [latitude] => 10
            [distance] => 5
        )

    [1] => Array
        (
            [latitude] => 5
            [distance] => 10
        )

    [2] => Array
        (
            [latitude] => 56
            [distance] => 48
        )

)



或者,您可以尝试找到SorterDarray实现,该实现将在元素插入数组时保持元素的排序,而不仅仅是在数组上调用
usort()
时。不过,对于这个问题来说,这有点太高级了。

在您的
while
循环之后尝试以下操作:

uasort($locationSort, 'sortBydistance');
// or uasort($locationSort, array($this, "sortBydistance")); if you are using withing a class
print_r($locationSort);

function sortBydistance($a, $b) {
    if ($a['distance'] == $b['distance']) {
        return 0;
    }
    return ($a['distance'] < $b['distance']) ? -1 : 1;
}
uasort($locationSort,'sortBydistance');
//或者uasort($locationSort,array($this,“sortBydistance”);如果你在课堂上使用
打印(位置排序);
函数排序距离($a,$b){
如果($a['distance']=$b['distance']){
返回0;
}
回报($a['distance']<$b['distance'])?-1:1;
}

不清楚您希望在这里完成什么。你能进一步回答你的问题吗?基本上我从数据库中获取位置,然后计算while循环中的距离。我现在需要做的是将变量“distance”添加到数组中。然后在该数组中按距离排序。我已经能够将距离添加到数组中,但无法使数组按距离排序。您可以向函数
usort
提供排序回调,该函数将允许您根据距离值进行排序。我已经尝试过,但我不明白如何按距离变量对其进行具体排序。警告:asort()期望参数2是长的,当我尝试此操作时给出字符串:asort($locationSort,'distance');打印(位置排序);查询运行良好,无需计算距离。我的问题是,我在while循环中按距离排序。最后,我不得不将“距离”变量添加到数组中。现在我在排序数组时遇到问题。。。使用距离变量。当我在排序之前打印数组时。。。距离变量在那里。但我不知道如何分类。。。通过新变量。不清楚他是否需要
uasort
,而不是
usort
。他的原始数组不是关联的。是的,但使用关联用户排序也没有什么坏处。从性能的角度来看,这会有坏处,但我认为这是一个可以忽略不计的损失,所以我同意。警告:uasort()希望参数2是一个有效的回调,未找到函数“sortBydistance”或函数名无效,然后出现致命错误:无法重新声明sortBydistance()(以前declared@RevoTech这个错误应该很容易理解。这个实例适用于复杂的数组吗?我的初始查询给出了大约15-20个变量,并扩展了该数组。我用这个示例替换了我的asort代码,它消除了错误,但似乎没有排序。你能通过使用
print\r($myArray);
以便查看其结构?