PHP: re order associative array
If you mean to swap two values you could make a function like this:
function array_swap($key1, $key2, $array) {
$newArray = array ();
foreach ($array as $key => $value) {
if ($key == $key1) {
$newArray[$key2] = $array[$key2];
} elseif ($key == $key2) {
$newArray[$key1] = $array[$key1];
} else {
$newArray[$key] = $value;
}
}
return $newArray;
}
How to sort a php associative array to a specific order?
Easy- Just use the arraySort
as the assoc key and get the corresponding array / value from the original array,
<?php
$arraySort = [
"break",
"period1",
"period2",
"period3",
"lunch",
"period4",
"period5",
"period6",
"finish"
];
$final_array = [];
foreach($arraySort as $arraySo){
$final_array[$arraySo] = isset($aData[$arraySo]) ? $aData[$arraySo] : [];
}
print_r($final_array);
Output:- https://3v4l.org/4LXvS
PHP re-order array of month names(associative array)
Make order array and then sort by usort functon
$seq = array_flip([ 'August', 'September', 'October', 'November' ]);
usort($arr, function ($i1, $i2) use($seq) { return $seq[$i1['label']] - $seq[$i2['label']]; });
demo
re arrange php associative array
You can use array_multisort
for your permutation. I assume that you know the permutation and don't need to derive it from the key names. Say, you want the order two, three, one
, then create a reference array like that:
$permutation = array(3, 1, 2);
Meaning: first item goes to position 3, second item to position 1, third item to position 2
Then, after the switch
, permute:
array_multisort($permutation, $example);
This will sort the $permutation
array and apply the same order to $example
.
php associative array key order (not sort)
Take a look at daniele centamore's comment on PHP's array_splice() function, where he provides a couple of functions for moving the elements in an non-associative array.
<?php
// $input (Array) - the array containing the element
// $index (int) - the index of the element you need to move
function moveUp($input,$index) {
$new_array = $input;
if((count($new_array)>$index) && ($index>0)){
array_splice($new_array, $index-1, 0, $input[$index]);
array_splice($new_array, $index+1, 1);
}
return $new_array;
}
function moveDown($input,$index) {
$new_array = $input;
if(count($new_array)>$index) {
array_splice($new_array, $index+2, 0, $input[$index]);
array_splice($new_array, $index, 1);
}
return $new_array;
}
$input = array("red", "green", "blue", "yellow");
$newinput = moveUp($input, 2);
// $newinput is array("red", "blue", "green", "yellow")
$input = moveDown($newinput, 1);
// $input is array("red", "green", "blue", "yellow")
?>
Sort associative array by key php
Create an array that defines the order you want.
$order = ['C' => 0, 'A' => 1, 'B' => 2];
Then pass that array into the usort
comparison function with use
. In the comparison function, look up the sort order in the $order
array by key using the letter from the items you're sorting.
usort($array, function($a, $b) use ($order) {
return $order[$a['letter']] <=> $order[$b['letter']];
});
You can also create the $sort
array without explicitly assigning numeric values using array_flip
$order = array_flip(['C', 'A', 'B']);
Sort an array of associative arrays by column value
You are right, the function you're looking for is array_multisort()
.
Here's an example taken straight from the manual and adapted to your case:
$price = array();
foreach ($inventory as $key => $row)
{
$price[$key] = $row['price'];
}
array_multisort($price, SORT_DESC, $inventory);
As of PHP 5.5.0 you can use array_column()
instead of that foreach:
$price = array_column($inventory, 'price');
array_multisort($price, SORT_DESC, $inventory);
Related Topics
Where to Use MySQL_Real_Escape_String to Prevent SQL Injection
Php: Equivalent of Include Using Eval
JSON_Decode() Returning Error "Notice: Trying to Get Property of Non-Object"
Mysqli Query to Loop Through Array and Update Multiple Rows
Best Debug Tool to Debug Ajax Request in PHP
Protect the Excel File Using PHPexcel
Problems with Secure Bind to Active Directory Using PHP
How to Change the Name of an Element in Dom
How to Determine If a PHP File Is Loaded via Cron/Command Line
Implementing "Update If Exists" in Doctrine Orm