Skip to content

Tag: sorting

Natural array sorting with proper decimal support in PHP

I want to sort an array with numbers in natural order, so numbers with a bigger value come after smaller ones like this: This is what I get as result, which is not the result I need, decimals are not threated correctly. There is already a similar question Array Sorting in php for decimal values but no fitting…

PHP topological sort

LIST: controller name => priority:required controllers sorted and expected output: First sort by priority, then reorder based on required controllers to be loaded before. ensure not to be stuck in an infinite loop due to controllers requiring each other. I assume I’ll need something like this: Please…

Sort an array so that a given value would be the first

I would like to sort the array so that if a given value ($first_in_array) = ‘brand’ value, the array would become the first it the queue. In the given example an array with index[1] should appear like at index[0]. Answer You can use the callback function of usort and first deal with the case where…

How to sort an Array using If in PHP

I have the following Array which contains data such as: Card, Month and Year. However, I receive it in a disorganized way and I would like to treat these values to follow the pattern. No Card, Year and Month or Month, Card and Year. I tried to use the sort() functions of php, but without success! I would like…

How can I sort an array stating in a given number

First of all I am new in php. I need help to sort an array. I have an array of ID’s and zip codes like this: Now I have a number lets say ‘2900’ I would like to sort my array from the closest number (2900) to the most distant. Example: the number is 2900. So the array should sort