Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP How to add to end of array?

Tags:

arrays

php

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

like image 238
vitalyp Avatar asked Jul 23 '11 01:07

vitalyp


People also ask

How do you add to the end of an array?

JavaScript Array push() The push() method adds new items to the end of an array. The push() method changes the length of the array. The push() method returns the new length.

How can I append an array to another array in PHP?

Given two array arr1 and arr2 and the task is to append one array to another array. Using array_merge function: This function returns a new array after merging the two arrays. $arr1 = array ( "Geeks" , "g4g" );

What is Array_keys () used for in PHP?

The array_keys() function returns an array containing the keys.


1 Answers

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";.

like image 119
Brad Avatar answered Oct 31 '22 23:10

Brad