Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fbc's avatar
Level 2

How do you add an element to an array without a key value?

I've been using the ARR helper to get this done but the problem is that I just want to add to the array without having to specify the key value..

$arr = []
$arr = Arr::add($arr,0,'Bob');
$arr = Arr::add($arr,0,'Larry');

only gets me:

array:1 [▼
  0 => "Bob"
]

in order to get Larry I have to do:

$arr = []
$arr = Arr::add($arr,0,'Bob');
$arr = Arr::add($arr,1,'Larry');

So I basically have to number them. i tried append instead of add but it didn't exist. Any other why to get this done??

0 likes
2 replies
fbc's avatar
Level 2

found it!

  $arr = array("black","blue");
  array_push($arr, "white");
Cronix's avatar
Cronix
Best Answer
Level 67

Or just

$data = [];

$data[] = 'one';

$data[] = 'two';

dd($data);
array:2 [
  0 => "one"
  1 => "two"
]

No real need for the array helper here really. It's more code than pure php anyway.

Please or to participate in this conversation.