This is a guide to Associative Array in PHP. Also, topics like advantages of the associative array and how to perform sorting on the associative array are also mentioned. Programs starting from basic like the syntax, creating the array, and traverse through the array are explained. krsort(): performs a sort on associative array according to the key in descending order ksort(): performs a sort on associative array according to the key in ascending orderĢ. In a similar way, the associative array can be sorted by key alphabetically both in ascending order and in descending order, as shown in the below example.ġ. Sorting of Associative Array by Key in PHPĪs associative array can be sorted by value in ascending order. arsort(): performs a sort on associative array according to the value in descending order asort(): performs a sort on associative array according to the value in ascending orderĢ. There are two inbuilt php functions like asort() and arsort(), which are used for sorting of the associative array by value in alphabetical order.ġ. Here we will learn about sorting the associative array by value. Sorting of Associative Array by Value in PHPĪn associative array can be sorted in two ways based on the key and based on value. The superglobal arrays like $_POST, $_GET, $_SESSION arrays also support associative arrays.These indexes are user-defined and can be changed accordingly.Indexes in the array are used, which are helpful in remembering the data.It is similar to the user list, stack, queue, etc.There are different functions that work to merge two associative arrays.for loop to traverse associative arrayįor($i=0 $i". Now to iterate through this loop, we will use for loop and print the keys and values as required. The array_keys function takes an input array as the parameter and outputs an indexed array. These keys are returned in the form of an array. Also, we will be using the array_keys function to get the keys of the array, which are father, mother, son, and daughter. In this example, we will use the same array family as in the previous example and traverse using for loop. first method to traverse the associative array To traverse this array, we use a foreach loop, in which we print both keys as Father, Mother, Son, Daughter and values as Mohan, Sita, Raj and Mona of the array. And each key holds the name of the relationship like the first value for index Father is Mohan, the second value for index Mother is Sita, the third value for index Son is Raj, the fourth value for index Daughter is Mona. This array is in the key-value form where the keys are names of relations like Father, Mother, Son, Daughter. In this example, an array is declared and named as a $family. One is the foreach loop, and the second is for a loop. There are two methods through which we can traverse the associative array. ?> How to Traverse Associative Array in PHP using various methods There are two ways to create an associative array.Ĭode: "Mohan", "mother"=>"Sita", "son"=> "Raj" ,"daughter"=> "Mona") The key value in the array is declared using the ‘=>’ arrow. The associative array is declared using an array keyword. How to Create an Associative Array in PHP?
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |