Array transposition PHPcode $ arrayarray (AB & gt; 123, ef & gt; 234, hc & gt; 654) I want to change hc and AB positions now, including (key and value) transposition results & nbsp; PHPcode $ arrayarray (hc & gt; 654, ef & gt; 234, array transposition problems
PHP code
$array = array( 'ab'=>123, 'ef'=>234, 'hc'=>654)
I want to change the hc and AB positions, including (key and value)
Result after transposition
PHP code
$array = array( 'hc'=>654, 'ef'=>234, 'ab'=>123,)
What methods can we do?
One method: declare a new array, loop $ array, and make a judgment to change the position!
I like to find a more efficient method. I am here to post for help.
------ Solution --------------------
Php array sorting ..................
You can use "krsort ()" in this example ()".
The rest of the cases are not certain.
------ Solution --------------------
$ Array = array (
'AB' = & gt; 123,
'Ef '=> 234,
'HC '=> 654
);
Echo"
";
Print_r ($ array );
Ksort ($ array, 1 );
Print_r ($ array );
------ Solution --------------------
Discussion
Reference:
Php array sorting ..................
You can use "krsort ()" in this example ()".
The rest of the cases are not certain.
It seems that krsort doesn't work. this is not just a sort, but a shift.
------ Solution --------------------
Uksort ()
Set rules by yourself.
------ Solution --------------------
The key is LZ. what rules should you use to change positions ??
$ Array = array (
'AB' = & gt; 123,
'Ef '=> 234,
'HC '=> 654,
& Apos; ht & apos; = & apos; 456 & apos,
'Ty '=> 421,
);
So should I change the AB hc position/ef ht position/hc ht position ???
You have a reference.
------ Solution --------------------
Array loops once, O (n) complexity, and efficiency problems?
------ Solution --------------------
I think foreach is OK. an array does not have any efficiency problems.