

The keys of the array are scott_Mcall, Stalenski, Lydia, Allision, and we used them to assign the age to each student.
#Php associative array index code
The code above is an example of an associative array. For example: 17,Įcho $student_age //this code will display the age of Scot_Mcall as 17Įcho $student_age //this code will display the age of stalenski as 18Įcho $student_age //this code will display the age of Lydia as 16Įcho $student_age //this code will display the age of Allision as 17 We can use the Associative array method to get it done. Suppose we want to assign ages to a group of high school students with their names.

Here's an example of a numeric array: įrom the code above I have a variable of $cars which stores an array of 5 elements. What are Numerical or Indexed Arrays?Ī numerical array is a type of array which can store strings, numbers, and objects. Let's look at how each one works in more detail. There are different types of arrays in PHP. So whenever you want to call the first value of an array you start with 0 then the next is 1.and so on. To create an array in PHP, we use the array function array( ).īy default, an array of any variable starts with the 0 index. To create a multidimensional array in PHP, you simply create an array of arrays.An array is a special variable that we use to store or hold more than one value in a single variable without having to create more variables to store those values.
#Php associative array index how to
How to create a multidimensional array in PHP? krsort(): sorts an associative array in descending order, according to the key.ksort(): sorts an associative array in ascending order, according to the key.arsort(): sorts an associative array in descending order, according to the value.asort(): sorts an associative array in ascending order, according to the value.rsort(): sorts the values of an array in descending order.sort(): sorts the values of an array in ascending order.The following are some of the functions you can use to sort arrays: Sorting is a common operation when working with arrays in PHP. You can then use these variables to perform some action for each element of the array. $key and $value are variables that will hold the key and value of the current element of the array, respectively. In the above code, $array is the name of the array you want to loop through. $fruits = array ( 'apple', 'orange', 'banana', 'grape' ) if ( in_array ( 'apple', $fruits ) ) Using the unset() function, you can remove a specific element of an indexed or associative PHP array by specifying its index or key respectively.

You can remove elements from an existing PHP array using the unset() function or the array_splice() function. How do I remove elements from an existing PHP Array? In this code snippet, we have added two new elements ( banana and grape) to the existing $fruits array using the array_push().Īlternatively, you can use square brackets notation by assigning a value to a new index position in an indexed array or setting a new key–value pair for associative arrays.įor example, to add element to indexed arrays, $num_array = 67 will add the value 67 at the end of the $num_array.Īs an example of adding an element to an associative array, $user_data = 'United States' will add a new key–value pair to the $user_data array. $fruits = array ( 'apple', 'orange' ) array_push ( $fruits, 'banana', 'grape' ) print_r ( $fruits ) // Output: Array ( => apple => orange => banana => grape)
