Array transposition problems

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.