0

I need to declare an empty array in php and add some elements to that array. I know this is possible to fill array in this way:

$list = array("value1", "value2", "value3");

But I want to have something like this:

$list = array();
$list->add("value1"); //pseudo-code 
// some other program code
$list->add("value2"); //pseudo-code 

Is there any way for doing that?

1

3 Answers 3

4

Alternatively:

$list = [];

// add values
$list[] = 'foo';

//add more...
$list[] = 'bar';
Sign up to request clarification or add additional context in comments.

Comments

1

There is the array_push method in PHP:

array_push($list, "value");

Comments

0

You are looking for Array_Push()

Here is an example of it being used:

$stack = array("orange", "banana");
array_push($stack, "apple", "raspberry");
print_r($stack);

outputs

Array
(
    [0] => orange
    [1] => banana
    [2] => apple
    [3] => raspberry
)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.