Following is the array that I want to sort:
// Sample array
$myArray = array(
"apple" => 2,
"orange" => 5,
"banana" => 3,
"kiwi" => 1,
);
The key that I want to keep on top is:
$specialKey = "banana";
Following is the array that I want to sort:
// Sample array
$myArray = array(
"apple" => 2,
"orange" => 5,
"banana" => 3,
"kiwi" => 1,
);
The key that I want to keep on top is:
$specialKey = "banana";
I can see that you want to sort keys, so here is a shorter way to do the same (as well as performance wise faster):
<?php
// Sample array
$myArray = array(
"apple" => 2,
"orange" => 5,
"banana" => 3,
"kiwi" => 1,
);
$specialKey = "banana";
$toparray[$specialKey] = $myArray[$specialKey]; //get key-values in new array
unset($myArray[$specialKey]);//unset key-value from original array
ksort($myArray); //sort original array based on key
print_r($toparray+$myArray);//combine both array to get desired result