How does php set the key of array to be equal to the value?-php Tutorial

Source: Internet
Author: User
How does php set the key of array to be equal to value? the following Array array ([0] & gt; content 1 [1] & gt; content 2 [2] & gt; content 3) the following Array (& quot; content 1 & quot; & gt; & quot; content 1 & quot;, & quot; content 2 & quot; & gt; & quot; content 2 & quot;, & php how to set the array key to be equal to the value
The following array is available:
Array ([0] => Content 1 [1] => content 2 [2] => Content 3)

The following array is expected to be produced after processing

Array ("content 1" => "content 1", "content 2" => "content 2", "Content 3" => "content 3 ")

------ Solution --------------------
Foreach traverses the array and then uses the for loop to loop with the key value.
------ Solution --------------------
PHP code
$ Arr = array (0 => 'Content 1', 1 => 'Content 2', 2 => 'Content 3'); foreach ($ arr as $ v) $ k [] = $ v; print_r (array_combine ($ k, $ arr ));
------ Solution --------------------
PHP code
$ Arr = array (0 => 'Content 1', 1 => 'Content 2', 2 => 'Content 3 '); $ r = array_combine (array_values ($ arr), $ arr); print_r ($ r );

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.