Forum : How do I add to the end of an array and know how large the array is?
Brief description  about Online courses   join in Online courses
View Arun  Desai 's Profile

How do I add to the end of an array and know how large the array is?

How do I add to the end of an array and know how large the array is???????
Asked by Arun Desai | Mar 17, 2010 |  Reply now
Replies (1)
View teacher siliconindia 's Profile
Hi Arun,
If you want to add an element to an array you can always just use:
$arrayname[] = "$value";
However, if you want to know how many entries are in the array once you've done so, then you can get at that value simply by using the array_push function, like so:
<?php
$values = array("I","Like","Sentences","That","Don't");
$newelement = array_push($values,"Finish");
echo "There are now $newelement elements in the array:\n";
print_r($values);
?>
This prints out the following to the screen:
There are now 6 elements in the array:
Array
(
[0] => I
[1] => Like
[2] => Sentences
[3] => That
[4] => Don't
[5] => Finish
)
Mar 17, 2010