Array_push () one value in multiple arrays

I understand that this can be a very simple question, but I need to know how to add a single value to multiple arrays in PHP. (The best way)

array_push($one, "hello"); array_push($two, "hello"); 

I need to do something like this (as an example)

 array_push($one && $two, "hello"); 

I read this question and saw a discussion, if $array[] better for speed, is it easier to use $array[] for my specific problem?

Thanks in advance! && please request the necessary clarifications!

+6
arrays push php
source share
3 answers

I think the best way to do this would be ...

 $one[] = $two[] = 'hello'; 

He works!

Update

BTW Any answers using array_push? - Trufa

Of course.

 $value = 'hello'; array_push($one, $value); array_push($two, $value); 

Although I would say that using the [] syntax is easier :)

If you want to add multiple elements to an array, it may be easier to use array_merge() .

 $one = array_merge($one, array( 'a', 'b', 'c' )); 

You can also use + array operaror , but it acts differently (for example, it will not overwrite string keys from the left operand array_merge() will be).

 $one += array( 'a', 'b', 'c' ); 
+4
source share

try $one[] = $two [] = "hello";

+2
source share

Why should it be on one line? The code below works and is very readable:

 $value = 'hello'; $one[] = $value; $two[] = $value; 
+2
source share

All Articles