How to sort the php two-dimensional array according to the order value in the one-dimensional array without changing the array order with the same order value?

how to sort the php two-dimensional array according to the order value in the one-dimensional array without changing the array order with the same order value

1. First grouping according to the order value, but they have to keep the previous order
2, then the grouping is sorted according to the order value from smallest to largest
3, and then assemble these arrays in a two-dimensional array

.

PS: can change the following first two-dimensional array into the second format

array(18) {
  [0] => array(3) {
    ["id"] => string(5) "ele_0"
    ["order"] => string(1) "1"
    ["title"] => NULL
  }
  [1] => array(3) {
    ["id"] => string(5) "ele_1"
    ["order"] => string(1) "1"
    ["title"] => NULL
  }
  [2] => array(3) {
    ["id"] => string(5) "ele_2"
    ["order"] => string(1) "1"
    ["title"] => NULL
  }
  [3] => array(3) {
    ["id"] => string(5) "ele_4"
    ["order"] => string(1) "3"
    ["title"] => NULL
  }
  [4] => array(3) {
    ["id"] => string(5) "ele_5"
    ["order"] => string(1) "2"
    ["title"] => NULL
  }
  [5] => array(3) {
    ["id"] => string(5) "ele_6"
    ["order"] => string(1) "2"
    ["title"] => NULL
  }
  [6] => array(3) {
    ["id"] => string(5) "ele_7"
    ["order"] => string(1) "2"
    ["title"] => NULL
  }
  [7] => array(3) {
    ["id"] => string(5) "11329"
    ["order"] => string(1) "2"
    ["title"] => NULL
  }
  [8] => array(3) {
    ["id"] => string(5) "11330"
    ["order"] => string(1) "7"
    ["title"] => NULL
  }
  [9] => array(3) {
    ["id"] => string(4) "8252"
    ["order"] => string(1) "1"
    ["title"] => NULL
  }
}
array(18) {
  [0] => array(3) {
    ["id"] => string(5) "ele_0"
    ["order"] => string(1) "1"
    ["title"] => NULL
  }
  [1] => array(3) {
    ["id"] => string(5) "ele_1"
    ["order"] => string(1) "1"
    ["title"] => NULL
  }
  [2] => array(3) {
    ["id"] => string(5) "ele_2"
    ["order"] => string(1) "1"
    ["title"] => NULL
  }
  [9] => array(3) {
    ["id"] => string(4) "8252"
    ["order"] => string(1) "1"
    ["title"] => NULL
  }
  [4] => array(3) {
    ["id"] => string(5) "ele_5"
    ["order"] => string(1) "2"
    ["title"] => NULL
  }
  [5] => array(3) {
    ["id"] => string(5) "ele_6"
    ["order"] => string(1) "2"
    ["title"] => NULL
  }
  [6] => array(3) {
    ["id"] => string(5) "ele_7"
    ["order"] => string(1) "2"
    ["title"] => NULL
  }
  [7] => array(3) {
    ["id"] => string(5) "11329"
    ["order"] => string(1) "2"
    ["title"] => NULL
  }
  [3] => array(3) {
    ["id"] => string(5) "ele_4"
    ["order"] => string(1) "3"
    ["title"] => NULL
  }
  [8] => array(3) {
    ["id"] => string(5) "11330"
    ["order"] => string(1) "7"
    ["title"] => NULL
  }
}
Php
Aug.09,2021

you can swap key and value ['id'], put key into value first, and then replace it back after sorting


actually sorting is the same as an one-dimensional array. If you apply any sorting algorithm, you can achieve the results you want.

< H2 > specific examples < / H2 >

sort the following array:

$arr = [
    [
        'id' => 'ele_0' , 
        'order' => 60 , 
    ] , 
    [
        'id' => 'ele_1' , 
        'order' => 30 , 
    ] , 
    [
        'id' => 'ele_2' , 
        'order' => 50 , 
    ] ,     
];

use Select sort algorithm:

for ($i = 0; $i < count($arr); PP$i)
{
    $index = $i;
    for ($n = $i + 1; $n < count($arr); PP$n)
    {
        $one = $arr[$index];
        $two = $arr[$n];
        // 
        if ($one['order'] < $two['order']) {
            $index = $n;
        }
    }
    if ($index != $i) {
        $tmp = $arr[$index];
        $arr[$index] = $arr[$i];
        $arr[$i] = $tmp;
    }
}
// 
print_r($arr);

$arr[] = array('order' => 1, 'id' => 'ele_0');
$arr[] = array('order' => 1, 'id' => 'ele_2');
$arr[] = array('order' => 1, 'id' => 'ele_1');
$arr[] = array('order' => 3, 'id' => 'ele_4');
$arr[] = array('order' => 2, 'id' => 'ele_6');
$arr[] = array('order' => 2, 'id' => 'ele_5');


foreach ($arr as $key => $row) {
    $order[$key]  = $row['order'];
    $arr[$key]['sort'] = $key;        //
    $sort[$key] = $key;
}

array_multisort($order, SORT_ASC, $sort, SORT_ASC , $arr);
print_r($arr);
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b37577-2c092.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b37577-2c092.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?