Php rotation of existing array keys-php Tutorial

Source: Internet
Author: User
Php replaces the existing array key value. This post was last edited by html5cloud in 2013-02-2513: 57: 00 & nbsp; foreach ($ arry1 & nbsp; as & nbsp; $ key & nbsp; & gt; & nbsp; $ value) & nbsp ;{& nbsp; if & php replaces existing array key values

This post was last edited by html5cloud at 13:57:00


Foreach ($ arry1 as $ key => $ value ){
If (array_key_exists ($ key, $ arry2 ))
{
$ Arry2 [$ key] = $ value;
}
Else
{
Array_push ($ arry2, "$ key => $ value ");
Ksort ($ arry2 );
}
}


Requirement: if arry2 contains the attribute in arry1, replace the attribute value in arry2 with the attribute value in arry1. if not, add it.
The array is like key => value, for example, arry [a: 1, B: [1, 2].
Why is there a problem with the above code?


------ Solution --------------------
Array_push ($ arry2, "$ key => $ value ");
Change
$ Arry2 [$ key] = $ value ");

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.