Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.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中作为结果获得的多维数组进行排序_Php - Fatal编程技术网

如何对PHP中作为结果获得的多维数组进行排序

如何对PHP中作为结果获得的多维数组进行排序,php,Php,从查询中,我得到了一个多维数组。稍后,我需要对该数组进行排序,以向用户提供响应。我想根据位置对数组进行排序。我无法使用foreach/for循环来完成它 代码: $getImages=$this->event_model->getpromotinaldataAll(); $getOffers=$this->event_model->getAllOffersdata(); foreach($getimagesas$row){ $tmp=array(); $tmp['id']=$row->pid;

从查询中,我得到了一个多维数组。稍后,我需要对该数组进行排序,以向用户提供响应。我想根据位置对数组进行排序。我无法使用foreach/for循环来完成它

代码:

$getImages=$this->event_model->getpromotinaldataAll();
$getOffers=$this->event_model->getAllOffersdata();
foreach($getimagesas$row){
$tmp=array();
$tmp['id']=$row->pid;
$tmp['name']=$row->partnername;
$tmp['latitude']=$row->latitude;
$tmp['longitude']=$row->longitude;
$tmp['image\u url']=$url.$row->image;
//$tmp['date']=$row->created\u at;
$tmp['type']=$row->type;
$tmp['position']=$row->position;
$tmp['objectId']=$row->couponId;
$tmp['actionTitle']=$row->actionTitle;
阵列推送($response,$tmp);
}
foreach($getOffers作为$row){
$tmp=array();
$tmp['id']=$row->id;
$tmp['name']=$row->name;
$tmp[‘纬度’]=“”;
$tmp[‘经度’]=“”;
$tmp['image\u url']=$url.$row->image;
//$tmp['date']=$row->date;
$tmp['type']=$row->type;
$tmp['position']=$row->position;
$tmp['objectId']=$row->eventId;
$tmp['actionTitle']=$row->actionTitle;
阵列推送($response,$tmp);
}
输出:

数组
(
[0]=>阵列
(
[id]=>2
[名称]=>盐海
[纬度]=>12.913510
[经度]=>77.487395
[image\u url]=>
[类型]=>1
[位置]=>2
[objectId]=>3
[actionTitle]=>创建邀请
)
[1] =>阵列
(
[id]=>1
[名称]=>索拉布酒店
[纬度]=>28.6466759
[经度]=>76.8123909
[image\u url]=>
[类型]=>1
[位置]=>4
[objectId]=>4
[actionTitle]=>创建邀请
)
[2] =>阵列
(
[id]=>5
[名称]=>试用
[纬度]=>
[经度]=>
[image\u url]=>
[类型]=>2
[位置]=>2
[objectId]=>4
[actionTitle]=>邀请我
)
[3] =>阵列
(
[id]=>6
[名称]=>试用
[纬度]=>
[经度]=>
[image\u url]=>
[类型]=>2
[位置]=>1
[objectId]=>4
[actionTitle]=>邀请我
)
)

请尝试以下代码:

<?php

$data = array_column($response, 'position');

// ascending order
ksort($data);

// descending order
krsort($data);

// if you want to reset the keys of the array
array_values($data);

?>

“从查询中,我得到了一个多维数组。”——最好的选择是在SQL中使用
排序依据
,并获得已经排序的结果

如果不可能,则使用。假设您的数组存储在变量
$data
中,则可以通过
位置的值对其进行排序:

array_multisort(array_column($data, 'position'), SORT_ASC, $data);
有关更多示例和说明,请阅读。

一种方法:

array_multisort(array_column($response, 'position'), SORT_ASC, $response);
第二种方法:

$pos = array();
                    foreach ($response as $key => $row)
                    {
                        $pos[$key] = $row['position'];
                    }
                    array_multisort($pos, SORT_ASC, $response); 

“从查询中,我得到了一个多维数组。”--最好的选择是在SQL中使用
排序依据
,并获得已经排序的结果。如果这是不可能的,那么使用.thank you.。它可以像我预期的那样工作。$pos=array();foreach($key=>$row的响应){$pos[$key]=$row['position'];}数组_multisort($pos,SORT_ASC,$response);即使是这段代码也运行得很好
array\u column()
为您实现了
foreach
,而且运行速度更快。如果您的PHP版本中不存在
array\u column()
(PHP 5.5中引入了它),则升级到PHP 7.2。旧版本是。我想你不能像你在问题中说的那样使用foreach,所以我在回答中使用了
array\u column