First, the $multiArray variable is defined as a multi-dimensional array that contains three sub-arrays, each with a name and age field.Here’s an example: $multiArray = array(Īrray_multisort($age, SORT_ASC, $name, SORT_ASC, $multiArray) To use this function to sort a multi-dimensional array, you need to extract the values of the arrays into separate arrays. The array_multisort() function is another built-in PHP function that sorts multiple arrays at once. This function displays the contents of the array in a human-readable format. print_r($multiArray) – Finally, this line prints the sorted multi-dimensional array to the screen using the print_r() function.In this case, it’s "compareAge", the function we defined earlier. The usort() function takes two arguments – the array to be sorted, and the name of the comparison function to be used. usort($multiArray, "compareAge") – This line uses the usort() function to sort the $multiArray array using the compareAge() function.Inside the function, the ages of the two arrays are subtracted from each other, and the result is returned. The function takes two arguments, $a and $b, which represent two inner arrays being compared. function compareAge($a, $b) – This defines a function called compareAge that will be used to compare the ages of the inner arrays in the multi-dimensional array. Each inner array contains two key-value pairs – one for name and another for age.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |