How to Sort Array of Objects by Property in PHP

sort array of objects by property in PHP
Sort Array of Objects by Property in PHP The article answers how to sort array of objects by property in PHP. Here's a quick overview of the solution. $employees_arr = [ new Employee('5', 'Brian', 90000, '25'), new Employee('1', 'Allen', 60000, '29'), new Employee('2', 'Kylie', 50000, '27'), new Employee('3', 'Jeane', 80000, '35'), new Employee('4', 'Franklin', 75000, '30'), ]; //Sorts the array by the property salary usort($employees_arr, function($a, $b) { return $a->salary - $b->salary; }); If the code doesn't make sense to you now, don't worry! Just stick with the article to the end, and you'll be able to understand all about the code above and perhaps a couple of other interesting PHP stuff! Plus, the article now includes sorting based on two or more properties. Check that out as well. Relevant Content: PHP usort Function The article uses the usort function to sort array of objects by property value in PHP.…

read more

How to sort associative array in PHP

sort associative array
In the article about how to sort an array in PHP,  we saw several functions for sorting an array. Some of these functions are useful for sorting an associative array, either by key or by value. Here’s a throwback to those functions. asort() – Sorts an associate array by value, in ascending order.arsort() – Sorts an associative array by value, in descending order.ksort() – Sorts an associate array by key, in ascending order.krsort() – Sorts an associate array by key, in descending order. In this article about how to sort associative array in PHP, we’ll extend upon the previous article and see some more advanced functions. These functions are usually helpful in sorting multi-dimensional associative arrays. We’ll be using an array of inventory items in the examples. Inventory Items Here’s how the array looks like. $inventory_items = array( array("ProductName"=>"White Sneakers","InStock"=>25,"Price(USD)"=>100), array("ProductName"=>"Metal Ring","InStock"=>15,"Price(USD)"=>50), array("ProductName"=>"Denim Jeans","InStock"=>10,"Price(USD)"=>40), array("ProductName"=>"Wooden Table","InStock"=>7,"Price(USD)"=>132), array("ProductName"=>"Gaming Chair","InStock"=>8,"Price(USD)"=>200), ); What…

read more

How to sort an array in PHP

sort an array in PHP
In this article, we’ll explore multiple functions to sort an array in PHP. As an overview, here are the functions that we are going to explore. sort() - Sorts an array in ascending order.rsort() - Sorts an array in descending order.asort() - Sorts an associate array by value, in ascending order.arsort() - Sorts an associative array by value, in descending order.ksort() - Sorts an associate array by key, in ascending order.krsort() - Sorts an associate array by key, in descending order. We have a lot to see so let’s jump straight to the topic. Option 1 - Sort an array in PHP using sort() The first function in the list is the sort function. This function sorts an array in ascending order. In the examples, we have called the sort function on names and numbers array to see both alphabetic and numeric sort. <?php //Array containing names. $names_array = array("Zeus","Tina","Robert","Benjamin","Stacie","Anna","Franklin");…

read more