Sorts specified items of a two-dimensional array.
- /*
- $ Array_name: input array;
- $ Row_id: the items to be sorted by the array;
- $ Order_type: sorting method, ASC or DESC;
- */
- Function array_sort ($ array_name, $ row_id, $ order_type ){
- $ Array_temp = array ();
- Foreach ($ array_name as $ key => $ value) {// loop layer;
- $ Array_temp [$ key] = $ value [$ row_id]; // create a one-dimensional array. The index value is the index value of a two-dimensional array; the value is the value of the project to be compared in a two-dimensional array;
- }
- If ($ order_type = "ASC "){
- Asort ($ array_temp );
- } Else {
- Arsort ($ array_temp );
- }
- $ Result_array = array ();
- Foreach ($ array_temp as $ key => $ value) {// iterate over the filtered array;
- $ Result_array [] = $ array_name [$ key]; // create a new result array. change the original input key-value sequence and assign the value to the result array (the original array remains unchanged );
- }
- Return $ result_array;
- }
- // The following is the test
- $ Arr = array ('num' => 7, 'value' => 4 ),
- Array ('num' => 3, 'value' => 45 ),
- Array ('num' => 38, 'value' => 27 ));
- $ Sortarr = array_sort ($ arr, 'value', 'asc ');
- Print_r ($ sortarr );
- ?>
|