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

PHP

// 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
var_dump($my_array);

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

// example output
array(5) {
  [0]=>
  string(7) "an item"
  [1]=>
  string(12) "another item"
  [2]=>
  string(10) "more items"
  [3]=>
  string(12) "more items 2"
  [4]=>
  int(12345)
}
*/

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 266 times.

Search Code
Search Code by entering your search text above.
Welcome

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
Don't look to be the next Facebook, try to solve a real problem instead.
Unknown