6

I realize this may be a very simple question but I need to know how to add ONE value to multiple arrays in PHP. (The better way)

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

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

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

I have read through this question and seen the discussion if whether $array[] is better for speed, is it easier to use $array[] for my specific problem?

Thanks in advance!! && please ask for any clarification needed!

5
  • BTW, that was me that posted that question linked :P Commented Nov 23, 2010 at 5:23
  • @alex lol had´n noticed that! Commented Nov 23, 2010 at 5:28
  • BTW Any answers using array_push? Commented Nov 23, 2010 at 5:28
  • See my update :) Commented Nov 23, 2010 at 5:31
  • Access the value twice using Symmetric Array Destructuring: [0 => $one[], 0 => $two[]] = ['hello'] Commented Dec 8, 2024 at 7:20

3 Answers 3

4

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

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

It works!

Update

BTW Any answers using array_push? – Trufa

Sure.

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

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

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

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

You can also use the + array operaror, but it acts different (e.g. won't overwrite string keys from the left operand like array_merge() will).

$one += array(
   'a',
   'b',
   'c'
);
Sign up to request clarification or add additional context in comments.

2 Comments

Alex thanks, but I´m actually doing that (in my code, the "hello" is just an example) for the array_push I just wanted to see if I could do it in one line, or most importantly using only ONE array_push
@Trua array_push() lets you append multiple members, but doesn't let you append those multiple members to multiple arrays on its own. It takes a reference to one array only.
2

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

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

5 Comments

Your delimiters for the string do not match (', ") - maybe you copied and pasted mine when I did the same thing first :P
I did copy/paste it to start with. Was working on other solutions (a loop over arrays, etc), then noticed the author seemed happy with your answer . I still don't see the point of this question - why does he "need" to put things on one line. I think readability always trumps saving lines of code.
Thanks, I wanted to "put it in one line" because I´m actually using array_push() so I didn't want to call it several times in different consecutive lines...
Yeah, readability is generally the most important thing. But I feel doing it like in my answer doesn't harm the readability much. Plus, you can skip the $value variable. :) +1 for your valid point of view.
the thing is that you answered my question using $array[] which makes it simple to add one value to multiple arrays but if i were to use the array push, I would have to do: array_push($one, "hello"); array_push($two, "hello"); ten times, this doesn't seem to make a lot of sense to me, maybe its readable but its not very "nice"...
2

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

4 Comments

@Trua I think it is fair to say that they probably didn't see my answer when posting.
You are actually right, I´m sorry about that. So strange.. alex's appeared first when I refreshed, but the timestamp is there! :)
well its a 2 mins diff.. hehe.. its your call trufa
I accepted alex's answer because now it is more complete. Thanks a lot anyway, you've got my +1 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.