Adding multiple values to an associative array in PHP in one call

23,409

Solution 1

Try this:

$post_values = array( 
    "x_login"           => "API_LOGIN_ID", 
    "x_tran_key"        => "TRANSACTION_KEY", 
); 

$array2 = array(
    "x_version"         => "3.1",  
    "x_delim_data"      => "TRUE",  
    "x_delim_char"      => "|",  
    "x_relay_response"  => "FALSE",  
    "x_state"           => "WA",  
    "x_zip"             => "98004"  
);

$result = $post_values + $array2;

Caution however: If the key already exists in $post_values it will not be overwritten.

Solution 2

In order to keep things nice and clean and in this case, simple, you might be better off using array_merge( )

I personally declare any arrays at the top of my class file, in order to make them globally accessible, only because I tend to keep methods free of array declaration (OCD I guess!)

So for me I have an example that might help you, it's something that works for me when needed to add/merge two arrays together:

protected $array1 = array (
  'basic'   => '1',
  'example' => '2',
  'for'     => '3'  
);

protected $array2 = array(
  'merging'     => '4',
  'two'         => '5',
  'associative' => '6',
  'arrays'      => '7',
  'mate'        => '8'
);

Then within your class file, you can use these arrays or any created arrays and merge whenever you want:

public function ExampleOne() 
{
  $firstArray = $this->array1;
  print_r($firstArray);

  $secondArray = $this->array2;
  print_r($secondArray);

  $merged = array_merge($firstArray, $secondArray);
  print_r($merged);
}

Each print_r( ) will give you a print out in the console of the data/created array. This is so you can view for yourself that everything has been created correctly and each key has its associated value (check the PHP man pages for a definitive explanation of print_r( ) ).

So, the first array will/should showcase this:

Array
(
  [basic]   => 1
  [example] => 2
  [for]     => 3
)

The second array will/should showcase this:

Array
(
  [merging]     => 4
  [two]         => 5
  [associative] => 6
  [arrays]      => 7
  [mate]        => 8
)

And the array_merge( ) operation will create the final array, which will/should showcase this:

Array
(
  [basic]       => 1
  [example]     => 2
  [for]         => 3
  [merging]     => 4
  [two]         => 5
  [associative] => 6
  [arrays]      => 7
  [mate]        => 8
)

Of course, you don't always have to place/create your arrays at the top of the class file and when needed you can obviously create arrays within a single function if they are only needed/used within there - what I showcased here was just something I had done recently for a project at work (with the data in these arrays being example data of course!)

Hope this helps people out a bit more :-D

Solution 3

array_push() will accept an array to be pushed.
But array_merge() may be more what you want.

Solution 4

You can try using the following function: array_merge

Share:
23,409
ViviDVasT
Author by

ViviDVasT

Updated on July 05, 2022

Comments

  • ViviDVasT
    ViviDVasT almost 2 years

    I am trying instantiate an associative array and then in a second call, assign it various other value sets on one block-line. I would like to do this following the same form as in the instantiation:

    "variable"  = > 'value';
    

    My instantiation is:

    $post_values = array(
        "x_login"           => "API_LOGIN_ID",
        "x_tran_key"        => "TRANSACTION_KEY",
    );
    

    I would like to add:

    "x_version"         => "3.1",
    "x_delim_data"      => "TRUE",
    "x_delim_char"      => "|",
    "x_relay_response"  => "FALSE",
    "x_state"           => "WA",
    "x_zip"             => "98004"
    

    What are my options? Perhaps there's an array_push usage that I don't know about to add multiple values with more ease? Or am i stuck adding on value per call like:

    $post_values['x_version']='3.1'; 
     ....
    $post_values['x_zip']='98004';
    

    Is there any other graceful way to do add multiple values to an associative array in one line?

  • ViviDVasT
    ViviDVasT over 12 years
    Seems array_merge overwrites, where + is the same function but does not overwrite. Got it, thanks :)
  • Baptiste Costa
    Baptiste Costa about 9 years
    You can also use += operator like so $post_values += ['x_version' => '3.1', 'x_zip' => 98004];