The PHP array

Category: PHP

We think you might be interested in this job:

The PHP array | C.S. Rhymes C.S. Rhymes HomeBooksNigel's Intranet AdventureHow NOT to make a WebsiteHow NOT to use a SmartphoneMy WorkThemesProjectsSitesStoriesSponsorsAboutBlogSponsorThe PHP arrayPHP FundamentalsPublished: Jul 3, 2021 by C.S. RhymesArrays are a useful toolt to store multiple values. You can access a specific value in an array using a key. By default, if you don’t specify keys, they will be numeric and start at zero (not one);$myArray=['Red','Blue','Green'];echo$myArray;// Redecho$myArray ;// Blueecho$myArray ;// GreenIf you try and use an array key that doesn’t exist then you will get a PHP notice.echo$myArray ;// Notice: Undefined offset: 3Alternative syntaxThe short way of defining an array is using square brackets, but it’s useful to know the other syntax too, with the word array and brackets.$myArray=array('Red','Blue','Green');Add a new value to an arrayYou can add a new value to an array using empty square brackets, which will add the new value to the end of the array.$myArray=['Red','Blue','Green'];$myArray[]='Yellow';var_dump($myArray);// array(4) { => string(3)"Red" => string(4)"Blue" => string(5)"Green" => string(6)"Yellow"}Removing a value from an arrayYou can remove an item from an array using unset().$myArray=['Red','Blue','Green','Yellow'];unset($myArray);var_dump($myArray);// array(3) { => string(3)"Red" => string(4)"Blue" => string(5)"Green"}You can also use array_splice() to remove an item from an array. This takes the array as the first argument, the offset or where to start, and the length or how many items you want to remove.$myArray=['Red','Blue','Green','Yellow'];array_splice($myArray,3,1);var_dump($myArray);// array(3) { => string(3)"Red" => string(4)"Blue" => string(5)"Green"}You can also specify a forth argument with array_splice() to add a replacement value to the array at the same time as removing values.$myArray=['Red','Blue','Green','Yellow'];array_splice($myArray,3,1,'Purple');var_dump($myArray);// array(4) { => string(3)"Red" => string(4)"Blue" => string(5)"Green" => string(6)"Purple"}Specifying keysYou can specify the keys if you want to, using the format ['key' => 'value'], defining this key has this value.$myArray=['red'=>'Red','blue'=>'Blue','green'=>'Green',];echo$myArray['red'];// RedMulti-dimensional arrayYou can also have a multi-dimensional array, an array that contains an array.

If we use the same example as above we can use a foreach loop to do this.foreach($myArrayas$colour){echo"{$colour['label']}: {$colour['hexcode']}";}// Red: #FF0000// Blue: #0000FF// Green: #00FF00Array functionsThere are many built in functions for PHP arrays so if you are using an array in your code you can probably find a function that will do what you need, or even combine multiple functions to achieve what you need.For more information on PHP functions, you can check out the array functions PHP documentation.PHPBeginnerCodeSharePlease enable JavaScript to view the comments powered by Disqus.Latest PostsThe PHP arrayArrays are a useful toolt to store multiple values.

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Community Partners