PHP Create Array - array()
PHP Create Array
PHP Create Array
To create an array in PHP, use array function(). Based on the type of values the function array() takes as argument for its elements, and the indexing it generates for its elements, we will discuss through multiple scenarios with example PHP program.
Create Array using array() function
The syntax of array() function is
array ([ mixed $... ] )
where for mixed parameter, you can provide any of these values
comma separated key => value pairs comma separated values
array() function returns a PHP array created using the arguments passed to it.
In the following example, we will create an array containing a mix of integers and strings.
PHP Program
In the following example, we will create an array with key => value pairs.
PHP Program
Create Two Dimensional Arrays
A two dimensional array is an array in which one or more values inside the array is another array.
PHP Program
Create PHP array Automatic Array Index for Elements
Automatic Array indexing is a mechanism in which index is generated for the array elements based on the index that we may or may not provide to the elements in the array. Based on the type of index we provide, there are many scenarios we can go through Automatic Array Indexing. We shall go through each of them with examples.
1. Index is omitted
In this scenario, we create an array of element with no index specified. In such case, PHP automatically creates an integer index for the elements. The integer index starts at 0, incrementing by one for the next elements.
In the following example, we created an array with elements, but not provided any index. So, the index of first element is 0, index of second element is 1, index of third element is 2, and so on.
PHP Program
Program Output
2. Index provided to first element
In this scenario, index is provided to first element in the array. In such case, the given index is taken as starting index and the next elements will be assigned with next integers.
Following is an example program where we created an array with elements, and gave an index of 5 to first element. So, the index of first element would be 5, the next element's is 6, and the next element's index is 7.
PHP Program
Program Output
3. Index provided to an element somewhere in the middle of array
In this scenario, index is provided to an element in the array whose position is not the first. In such case, this index acts as starting index from this element's position and the next elements will get next integers as their index. But for the elements prior to that element, the first case "index is omitted" will be applied.
PHP Program
Program Output
4. Same Index provided to multiple elements in array
In this scenario, same index is given to multiple elements in the array. In such case, the later key => value pair is considered to update the previous element with the same index. PHP Program Program Output
Let us break down this scenario and understand how indexing is given to the elements. For the first two elements, no index is provided. So, their index start with zero. Hence 0 for "apple", and 1 for "mango".
We have given an index of 6 for "cherry". There is no index for "fig", so increment previous index by one and assign it to "fig". Now, "mango" has an index of 6, but we already have an element with index 6. So, the existing element with index 6 will be updated with the new value "mango". As a result, "mango" replaces "cherry". "orange" and "grape" do not have index, so they get an incremented index from the previous element. And the index would be 8 and 9 respectively. Concluding this scenario, when different elements got same index, the later will update the prior one.
5. Non integer Index
In this scenario, we provide index of type string to some of the elements. The elements which do not have get an integer index based on the first scenario "Index is omitted". PHP Program
Program Output
Conclusion
In this PHP Tutorial, we learned how to create a PHP array using array() function.
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
Related searches
- how to create an array in java
- how to create an array in python
- create an array java
- create np array of zeros
- matlab create array of strings
- create np array of size
- numpy create array of nan
- create numpy array python
- create empty array python
- numpy create array of zeros
- create uint8 array in matlab
- php create multidimensional array dynamically