vitalyp
vitalyp

Reputation: 691

PHP How to add to end of array?

I'm having difficulty adding to the end of an array. Not sure how to do it. Please help.

$person = array();
$person = array("name"=>"tom", "age"=>20, "height"=>180);

How do I add to the end of an array? I want to add "weight"=>120 to the end of an existing array.

Thanks

Upvotes: 19

Views: 83662

Answers (1)

Brad
Brad

Reputation: 163234

Since this is an associative array, just do:

$person['weight']=120;

For regular numerically indexed arrays, you can use array_push() or $person []= "new value";.

Upvotes: 40

Related Questions