PHP Arrays are like the ultimate variable of PHP. It is also acts as a storage for a collection of elements. Arrays can contain other variables like strings, integers, or even other arrays in a list type format. Arrays are often used whenever you have an unknown amount of variables that you need to store for retrieval later. PHP array values can be outputted by iterating though them using a loop, or you can simply call a specific element by its index or key value. The key value of an array can be an implicit indexing system if you don’t provide the key. You can see more about the keys or indexes later in this tutorial. Arrays an asset to any programmer wanting more dynamic variables. Let’s look on how to construct an array in PHP.
We have our array $numberList with individuals elements of 1, 3, and 7. We reference the elements individually when we print them. The print_r($numberList[0]); prints out 1 because our element with an index of 0 in our array is 1. Since we didn’t provide a key value when we created the array, PHP allows us to retrieve the elements using a numerical index. Notice that we didn’t have to define our arrays upfront with PHP.
If you come from another language, you might take note that arrays in PHP are not fixed. Our new array is completely identical to the array in the previous example. This is just another way to add elements to our PHP array. I am just showing you this so I can open you up to the idea of dynamic adding elements to an array. So let’s use this new technique to create dynamic arrays.
The Dynamic Variable (Arrays)
The way print_r prints an array is a little bit odd. All it does is tell you the index or key of the element and then the elements value. What you did here was dynamically create an array with values of 0-9 in that order. We created a for loop and each time we iterated, or went through the loop, we added a new key and value pair to our new array. Congratulations, you have learned a crucial lesson in the dynamic world of programming.
Common PHP Array Functions
I am going to run through some commonly used array functions.
array_pop() – removes the last element
array_push() – adds element to the end
array_search() – searches for value and returns its index
array_shift() – removes the first element and returns it
array_slice() – returns selected elements
array_splice() – replaces specified elements with new
array_unique() – removes duplicate values
array_unshift() – adds element to the beginning
count() – counts all elements
sort() – sorts the array
The functions above are some of the most commonly used array functions; however, you can find a complete list at PHP References. PHP arrays are an extremely powerful asset and are very useful when you are working with a few variables that deal with the same programming idea. They provide you with the functionality of not having to create a ton of single variables that pollute your code with a bunch of variables that are already used. They also give you the added benefit of not having to remember all of those variable names. Arrays can become complex when you get into arrays of arrays, but for now you should stay away from that headache.
nice array description. these all function very useful for web devlopment Area.
hi
array_slice() example incorrect
$eArray = array(“a”,”b”,”c”);
print_r(array_slice($eArray,1,1));
The third parameter should be 2
I’m not sure how the array_shift() works. Why it didn’t just return [0] => b [1] =>c, after removing first element?
$eArray = array("a","b","c");
print_r(array_slice($eArray,1,1));
o/p: Array ( [0] => b )
please correct it