Posted in php
3:10 am, May 13, 2021

adding an item to an array with php

how to add items to an array in php

here is a working sandbox version of this code: sandbox


// the array
$my_array = array();

// add an item
$my_array[] = "an item";

// add another item
$my_array[] = "another item";

// you can also add items with array_push, but im not sure why you would want to unless you are adding more than one item.
array_push($my_array, "more items");

// multiple items
array_push($my_array, "more items 2", 12345);

// dump the array to see what it has in it

// loop through the array and do things with the content
foreach ($bugs_array as $key => $value) {
  // code...

// example output
array(5) {
  string(7) "an item"
  string(12) "another item"
  string(10) "more items"
  string(12) "more items 2"

External Link for adding an item to an array with php

No Items Found.

Add Comment
Type in a Nick Name here
Other Items in php
rtrim strip white space or strings from the end of a string get the current month as a number show the difference between two dates in years, months, days, hours and seconds counting the occurrence of words in a multidimensional array write string contents to a file with php unable to access the $_FILES when submitting a form php check column exists in table sqlite show all methods or functions in a class (class function version) show all methods (functions) in a class adding an item to an array with php passing in arrays to your functions rather than using variables check server https or http value in php using $_SERVER Remove background image php with image magic get current url with php list a functions variables or arguments php list all the functions in a class php print an array nicely add a toast alert when logging in with half moon dont use md5 for password hashing using password_hash and password_verify php basic page router checking for spam comments function calculate a percentage (dec) difference based on two numbers explode a string into an array using gzdeflate on a string php include tinymce only if a user is logged into the site Quick tool to convert time into decimal generating all css colours with php? php countries array to dropdown form element Fix for Fatal error: Uncaught Error: Call to undefined function file_get_dom() simplehtmldom simple_html_dom Fixing PHP SQLite database is locked warning - Unable to execute statement: database is locked simple usage for php password_hash change string to capital words ucwords change upper case case string to lower case (uppercase, lowercase) convert a string to uppercase with php php substr return part of a string php countries array check if a timestamp is older than one hour making var_dump look nice php mysql table exists function how to display a php file as html with php php convert date now into a sql timestamp Generate a Random User Name return a state name from an australian postcode get the average of a number list using an array in php IP Address PHP Finding the position of a string in another string using strpos PHP str_replace - find and replace part of a string Random string generator function in php suniqid using the disk free space function in php create a mysql or sqlite current timestamp with php
Page Views

This page has been viewed 220 times.

Search Code
Search Code by entering your search text above.

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.


Random Quote
In this case my anchor this week becomes driving almost 2hrs outside of Atlanta to one of my favorite hard core gyms in the world.. MetroFlex aka The Dungeon. The gym owners turn the heat way up so it becomes a fun sweat box and the gym members just watch from afar and leave me alone. I happily drive myself long distances to find MY ANCHOR. Our anchor allows us to have balance, focus and be as productive as possible. And if you're in the middle of a heavy set and your headphones start to fall off your head, like mine did here.. well.. f*ck the headphones. Let em break and fall. You can always get a new pair, but the iron ain't ever gonna lift itself.