在一次调用中将多个值添加到PHP中的关联数组中

时间:2022-08-26 14:11:33

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:

我有什么选择?也许有一个array_push用法,我不知道要更轻松地添加多个值?或者我是否坚持增加每次通话的价值,如:

$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?

有没有其他优雅的方法可以在一行中为关联数组添加多个值?

4 个解决方案

#1


6  

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.

但是请注意:如果密钥已存在于$ post_values中,则不会被覆盖。

#2


2  

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

array_push()将接受要推送的数组。但是array_merge()可能更符合你的要求。

#3


2  

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

为了保持良好和干净,在这种情况下,简单,你可能最好使用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!)

我个人在我的类文件的顶部声明任何数组,以使它们全局可访问,只是因为我倾向于保持方法没有数组声明(OCD我猜!)

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( ) ).

每个print_r()都会在数据/创建数组的控制台中打印出来。这样您就可以自己查看所有内容都已正确创建,并且每个键都有其关联值(请查看PHP手册页以获取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_merge()操作将创建最终数组,它将/应该展示这个:

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

希望这可以帮助人们更多:-D

#4


1  

You can try using the following function: array_merge

您可以尝试使用以下函数:array_merge

#1


6  

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.

但是请注意:如果密钥已存在于$ post_values中,则不会被覆盖。

#2


2  

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

array_push()将接受要推送的数组。但是array_merge()可能更符合你的要求。

#3


2  

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

为了保持良好和干净,在这种情况下,简单,你可能最好使用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!)

我个人在我的类文件的顶部声明任何数组,以使它们全局可访问,只是因为我倾向于保持方法没有数组声明(OCD我猜!)

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( ) ).

每个print_r()都会在数据/创建数组的控制台中打印出来。这样您就可以自己查看所有内容都已正确创建,并且每个键都有其关联值(请查看PHP手册页以获取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_merge()操作将创建最终数组,它将/应该展示这个:

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

希望这可以帮助人们更多:-D

#4


1  

You can try using the following function: array_merge

您可以尝试使用以下函数:array_merge