I want to add an element c => 3 to each value. Is there any system function?-php Tutorial

Source: Internet
Author: User
I want to add an element c & gt; 3 to each value. Is there any system function? I want to add an element c => 3 to each value. can I skip foreach, is there any system function?
$ Ary =
Array
(
[0] => Array
(
[A] => 1
[B] => 2
)
[1] => Array
(
[A] => 1
[B] => 2
)
...
)
Objectives:
Array
(
[0] => Array
(
[A] => 1
[B] => 2
[C] => 3
)
[1] => Array
(
[A] => 1
[B] => 2
[C] => 3
)
...
)


Reply to discussion (solution)

First, the foreach large array, and then the array_push in The foreach.

First, the foreach large array, and then the array_push in The foreach.
I don't want to use foreach.

I tried to use array_pad, but it only fills in and cannot be added...

$ Ary = array ('a' => 1, 'B' => 2), array ('a' => 1, 'B' => 2 ), array ('a' => 1, 'B' => 2),); // method 1 $ res = array_map ('Array _ merge', $ ary, array_fill (0, count ($ ary), array ('C' => 3); print_r ($ res); // method 2 function foo2 (& $ v) {$ v ['c'] = 3 ;}$ res = $ ary; array_walk ($ res, 'foo2'); print_r ($ res ); // method 3 function foo3 (& $ v, $ k, $ param) {$ v = array_merge ($ v, $ param);} $ res = $ ary; array_walk ($ res, 'foo3', array ('C' => 3); print_r ($ res);/*** you can use a closure after php5.3 **/

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.