如何对在PHP中获得的多维数组进行排序

时间:2019-03-26 05:18:03

标签: php

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

代码:

$getImages = $this->event_model->getpromotinaldataAll();
    $getOffers = $this->event_model->getAllOffersdata();

    foreach($getImages as $row) {
        $tmp = array();
        $tmp['id'] = $row->pid;
        $tmp['name'] = $row->partnername;
        $tmp['latitude'] = $row->latitude;
        $tmp['longitude'] = $row->longitude;
        $tmp['image_url'] = $url.$row->image;
        //$tmp['date'] = $row->created_at;
        $tmp['type'] = $row->type;
        $tmp['position'] = $row->position;
        $tmp['objectId'] = $row->couponId;
        $tmp['actionTitle'] = $row->actionTitle;
        array_push($response, $tmp);
    }

    foreach($getOffers as $row) {
        $tmp = array();
        $tmp['id'] = $row->id;
        $tmp['name'] = $row->name;
        $tmp['latitude'] = "";
        $tmp['longitude'] = "";
        $tmp['image_url'] = $url.$row->image;
        //$tmp['date'] = $row->date;
        $tmp['type'] = $row->type;
        $tmp['position'] = $row->position;
        $tmp['objectId'] = $row->eventId;
        $tmp['actionTitle'] = $row->actionTitle;
        array_push($response, $tmp);
    }

输出:

Array
(
    [0] => Array
        (
            [id] => 2
            [name] => Saltocean
            [latitude] => 12.913510
            [longitude] => 77.487395
            [image_url] => 
            [type] => 1
            [position] => 2
            [objectId] => 3
            [actionTitle] => Create Invite
        )

    [1] => Array
        (
            [id] => 1
            [name] => saurabh hotel
            [latitude] => 28.6466759
            [longitude] => 76.8123909
            [image_url] => 
            [type] => 1
            [position] => 4
            [objectId] => 4
            [actionTitle] => Create Invite
        )

    [2] => Array
        (
            [id] => 5
            [name] => trial
            [latitude] => 
            [longitude] => 
            [image_url] => 
            [type] => 2
            [position] => 2
            [objectId] => 4
            [actionTitle] => Invite Me
        )

    [3] => Array
        (
            [id] => 6
            [name] => trial
            [latitude] => 
            [longitude] => 
            [image_url] => 
            [type] => 2
            [position] => 1
            [objectId] => 4
            [actionTitle] => Invite Me
        )
)

3 个答案:

答案 0 :(得分:1)

尝试以下代码:

<?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);

?>

答案 1 :(得分:0)

“从查询中我得到了多维数组。” -最好的选择是在SQL中使用SORT BY并获得已经排序的结果。

如果这不可能,请使用array_multisort()。假设您的数组存储在变量$data中,这就是按照position的值对数组进行排序的方式:

array_multisort(array_column($data, 'position'), SORT_ASC, $data);

有关更多示例和说明,请阅读documentation of array_multisort()

答案 2 :(得分:0)

一种方法:

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);