![]() The function compares the two sub-arrays by their key values specified in the $key parameter passed to the sortArray() function. This function takes two arguments, $a and $b, which represent the two sub-arrays being compared. Define a compare() function: Within the sortArray() function, a nested function named compare() is defined.Define a sortArray() function: The sortArray() function is defined next, which takes two arguments – an array to be sorted, and the key by which to sort the array.Each sub-array represents a person, with a name and an age. Create a multi-dimensional array: The first line of the code creates a multi-dimensional array, named $multiArray, which contains three sub-arrays, each with two key-value pairs.$sortedArray = sortArray($multiArray, 'age') You can also create a custom sort function that sorts a multi-dimensional array by value. Finally, the sorted $multiArray is printed using the print_r() function.The SORT_ASC constant is used to specify that the sorting order should be ascending.This function sorts multiple arrays at once, in this case, the $age and $name arrays. The array_multisort() function is called with the $age and $name arrays as arguments.Inside the loop, two separate arrays are created to store the name and age values of each sub-array. Then, a foreach loop is used to iterate over the sub-arrays in $multiArray.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. ![]() $multiArray = array(.) – This line initializes a multi-dimensional array called $multiArray, which contains three inner arrays.Here is an explanation of above-given example code: ![]() ![]() To use this function to sort a multi-dimensional array, you need to define a function that will compare the values of the arrays. The usort() function is a built-in PHP function that sorts an array by a specified function. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |