Is the php Index Array processed as an associated array as needed?

Source: Internet
Author: User
Some of the problems may not be well described. I will give you a simple example of {code...}. The above method can meet my needs. What other methods can be implemented? I found a built-in function array_chunk ($ arr, 1, true), but this key name cannot be set. The problem may not be well described. Let me give you a simple example.

// Requirement $ arr = array ('20160301', '20160301', '20160301', '20160301', '20160301 '); $ newArr = array ('id' => '123'), array ('id' => '123 '), array ('id' => '20140901'), array ('id' => '20160901'), array ('id' => '20160901 '), array ('id' => '000000'); // my processing method is $ newArr = []; foreach ($ arr as $ val) {$ newArr [] = array ('id' => $ val );}

The above method can meet my needs. What other methods can be implemented? I found a built-in function.array_chunk($arr, 1, true)However, this key name cannot be set.

Reply content:

The problem may not be well described. Let me give you a simple example.

// Requirement $ arr = array ('20160301', '20160301', '20160301', '20160301', '20160301 '); $ newArr = array ('id' => '123'), array ('id' => '123 '), array ('id' => '20140901'), array ('id' => '20160901'), array ('id' => '20160901 '), array ('id' => '000000'); // my processing method is $ newArr = []; foreach ($ arr as $ val) {$ newArr [] = array ('id' => $ val );}

The above method can meet my needs. What other methods can be implemented? I found a built-in function.array_chunk($arr, 1, true)However, this key name cannot be set.

The array_map () function can meet your needs:

$ Arr = array ('000000', '000000', '000000', '000000', '000000'); function make ($ v) {return array ('id' => $ v) ;}$ newArr = array_map ('make', $ arr); var_dump ($ newArr ); // thank you for choosing $ newArr = array_map (function ($ v) {return array ('id' => $ v) ;}, $ arr );

In addition, you can use anonymous functions to make them more elegant.

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.