-1

From query I got a multidimensional array. Later I need to sort this array to provide a response to the user. I want to sort the array based on position. I'm not able to do it using a foreach / for loop.

Code:

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

Output:

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
        )
)
karel
  • 5,489
  • 46
  • 45
  • 50
vaishu
  • 1
  • 7
  • *"From query I got a multidimensional array."* -- the best option is to use `SORT BY` in SQL and get the results already sorted. If this is not possible then use [`array_multisort()`](https://www.php.net/manual/en/function.array-multisort.php). – axiac Mar 26 '19 at 06:36

3 Answers3

1

Try the following code:

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

?>
user2203703
  • 1,955
  • 4
  • 22
  • 36
0

"From query I got a multidimensional array." -- the best option is to use SORT BY in SQL and get the results already sorted.

If this is not possible then use array_multisort(). Assuming your array is stored in the variable $data, this is how you can sort it by the values of position:

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

For more examples and explanation how this works, read the documentation of array_multisort().

axiac
  • 68,258
  • 9
  • 99
  • 134
  • thank you..it works as i expected. – vaishu Mar 26 '19 at 08:27
  • $pos = array(); foreach ($response as $key => $row) { $pos[$key] = $row['position']; } array_multisort($pos, SORT_ASC, $response); – vaishu Mar 26 '19 at 08:27
  • even this code worked well – vaishu Mar 26 '19 at 08:27
  • `array_column()` does the `foreach` for you and it works faster. If `array_column()` does not exist on your PHP version (it has been introduced in PHP 5.5) then upgrade to PHP 7.2. Older versions are [outdated and not maintained any more](https://www.php.net/supported-versions.php). – axiac Mar 26 '19 at 09:06
0

one method :

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

second method:

$pos = array();
                    foreach ($response as $key => $row)
                    {
                        $pos[$key] = $row['position'];
                    }
                    array_multisort($pos, SORT_ASC, $response); 
vaishu
  • 1
  • 7
  • I thought that you not able to do it using a foreach like you said in the question, so i used `array_column` in my answer. – user2203703 Mar 26 '19 at 08:58