Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8754983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:45:09+00:00 2026-06-13T13:45:09+00:00

I have a 2-dimensional array: $test = array( foo => array( ‘a’ => 1,

  • 0

I have a 2-dimensional array:

$test = array(
    "foo" => array(
        'a' => 1,
        'b' => 2,
        'c' => 3
    ),
    "bar" => array(
        'a' => 1,
        'b' => 2,
        'c' => 3
    ),
    "baz" => array(
        'a' => 1,
        'b' => 2,
        'c' => 3
    )
);

I would like to add a field named 'd' with the value 4 to each element of the outer array, so that the resulting array becomes:

array(
    "foo" => array(
        'a' => 1,
        'b' => 2,
        'c' => 3,
        'd' => 4
    ),
    "bar" => array(
        'a' => 1,
        'b' => 2,
        'c' => 3,
        'd' => 4
    ),
    "baz" => array(
        'a' => 1,
        'b' => 2,
        'c' => 3,
        'd' => 4
    )
)

I’ve tried this:

foreach ( $test as $elem ) 
{
    $elem['d'] = 4;
}

which doesn’t work. What am I doing wrong, and how can I make this work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:45:10+00:00Added an answer on June 13, 2026 at 1:45 pm

    Arrays and primitives are passed by value in PHP (though objects are passed by reference). One method to overcome this in a foreach loop is to access the subarrays by reference in the loop:

    // Call $elem by reference with &
    foreach ( $test as &$elem ) {
        $elem['d'] = 4;
    }
    
    print_r($test);
    array(3) {
      ["foo"]=>
      array(4) {
        ["a"]=>
        int(1)
        ["b"]=>
        int(2)
        ["c"]=>
        int(3)
        ["d"]=>
        int(4)
      }
      ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multi dimensional array, like this: array('name' => array('title'=>'Title','date'=>'Created')) I store it
I have this combination of multi-dimensional array but I can't print the correct value
I have a four dimensional array To access one value from my array I
I have 2-dimensional array like below. unsigned char myArray[][48] = { {0xfc,0x94,0x88,0x48,0x5f,0xa4,0x9a,0xfb,0x6e,0xf8,0xcd,0x01,0x47,0x64,0x03,0xd0,0x1f,0xb8,0xa3,0x85,0x84,0xa9,0x4a,0xc4,0x9e,0xea,0x26,0x09,0x62,0x96,0x91,0xa6}, {0xa9,0xc5,0x9a,0xb3,0x09,0x38,0x15,0xb3,0x22,0xb3,0x07,0x21,0x3e,0x39,0x35,0xc6,0x69,0x6e,0xf3,0x64,0xb0,0x0a,0x4c,0xcb,0x77,0xff,0x76,0x3c,0x37,0xf3,0x99,0x96}, {0x24,0x4d,0xc0,0x45,0xe0,0x50,0x1f,0x72,0x0f,0xb0,0xcc,0xb9,0xc6,0x72,0xa9,0x5a,0xf3,0x5a,0xd9,0xe2,0xc3,0x44,0xd9,0x25,0xf3,0x12,0x6a,0x0c,0x37,0x6a,0x3f,0xb6},
I have a 2 dimensional array, like so: char[,] str = new char[2,50]; Now,
I have following 2 dimensional array input: var arr = [ ['A', ['Sun','Moon']], ['B',
I have a multi-dimensional array, which basically consists of one sub-array for each year.
I have a multi-dimensional array, no problem. How do I interrogator one of the
I have an 2 dimensional array. I write 3 values into the array during
I have a multi dimensional array. The only actual values (other than other arrays)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.