Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php数组中执行类似mysql的排序?_Php_Arrays_Sorting_Multidimensional Array - Fatal编程技术网

如何在php数组中执行类似mysql的排序?

如何在php数组中执行类似mysql的排序?,php,arrays,sorting,multidimensional-array,Php,Arrays,Sorting,Multidimensional Array,但是,我如何获得拥有最高积分的人的id? 基本上我需要得到点最大的行(或内部数组) 仅使用php,如何在这个数组中执行类似mysql的排序 请提供帮助您可以将它们组合在一起,但以下是一般步骤: $myarray= Array ( [0] => Array ( [id] => 4 [name] => ABC [point] => 2111 ) [1] => Arr

但是,我如何获得拥有最高积分的人的
id
? 基本上我需要得到点最大的行(或内部数组)

仅使用
php
,如何在这个数组中执行类似mysql的排序


请提供帮助

您可以将它们组合在一起,但以下是一般步骤:

$myarray=
Array ( [0] => Array ( [id] => 4
                       [name] => ABC
                       [point] => 2111 
      ) [1] => Array ( [id] => 5
                       [name] => XYZ 
                       [point] => 1305 )




$points = array_map(function($myarray) {
        return $store[0];
    }, $myarray);

 $maxpoint=max($points)
另一个选项是按
降序排序,使其始终为索引0:

// extract all points and get the max()
$maxpoint = max(array_column($myarray, 'point'));

// get the key of the array with the max points
$key = array_search(array_column($myarray, 'point'), $maxpoint);

// get the id using the key
$id = $myarray[$key]['id'];

// or get the entire array
$result = $myarray[$key];

您可以将它们组合在一起,但以下是一般步骤:

$myarray=
Array ( [0] => Array ( [id] => 4
                       [name] => ABC
                       [point] => 2111 
      ) [1] => Array ( [id] => 5
                       [name] => XYZ 
                       [point] => 1305 )




$points = array_map(function($myarray) {
        return $store[0];
    }, $myarray);

 $maxpoint=max($points)
另一个选项是按
降序排序,使其始终为索引0:

// extract all points and get the max()
$maxpoint = max(array_column($myarray, 'point'));

// get the key of the array with the max points
$key = array_search(array_column($myarray, 'point'), $maxpoint);

// get the id using the key
$id = $myarray[$key]['id'];

// or get the entire array
$result = $myarray[$key];

被接受的答案没有问题,(事实上,这很好),但是如果你只需要得分最高的人,你不需要对数组排序。您可以迭代它,并在运行时跟踪最高指针

array_multisort(array_column($myarray, 'point'), SORT_DESC, $myarray);

echo 'id ' . $myarray[0]['id'] . ' has ' . $myarray[0]['point'] . ' points';

被接受的答案没有问题,(事实上,这很好),但是如果你只需要得分最高的人,你不需要对数组排序。您可以迭代它,并在运行时跟踪最高指针

array_multisort(array_column($myarray, 'point'), SORT_DESC, $myarray);

echo 'id ' . $myarray[0]['id'] . ' has ' . $myarray[0]['point'] . ' points';

@Don'tPanic的可能复制品:如果他们询问如何排序,我会同意,尽管这可能是最好的方法。@abracadver我不想争论,但他们不是吗?“在这个数组中,仅使用php如何才能执行类似mysql的排序?”@Don'tPanic:是的,我想我本可以阅读标题,抱歉:-(如果他们询问如何排序,我会同意,尽管这可能是最好的方法。@Abracadver我不想争论,但他们不是吗?“在这个数组中,如何仅使用php就可以执行类似mysql的排序?”@Don'tPanic:是的,我想我应该读一下标题,对不起:-(