Questions tagged [usort]

a function in PHP which provides means for custom collections sorting by using an user-defined comparison function to determine the order of elements

1
vote
0answers
15 views

how to sort folders first with scandir and using usort [duplicate]

For a filemanagement, I use create_function to sort folders(directories) first and then files. But it seems that create_function is deprecated in php 7.2. So how can I use the usort below correctly? ...
1
vote
0answers
20 views

how to sort directories above files in php

For a filemanagement, I used to render the folders and files with scandir and a foreach and sort them: Directories first, then Files $files = array_diff( scandir($dir), array(".", "..", "tmp") ); ...
2
votes
1answer
75 views

Usort on Array of DateTime Objects in PHP - No Error, Array Not Sorting

I have a long multi dimensional array including some DateTime objects. I want to sort the entire array based on the [startdate] object. I'm using this usort function (based on a lot of online ...
0
votes
1answer
35 views

alphabetic sorting puts shorter string after longer string in error

when sorting my array of strings, usort is putting the shorter string before the longer one, this is in odds to other sort methods, specifically the one on the client side of my app. for example. '...
1
vote
2answers
62 views

Unknown “usort() expects parameter 1 to be array, null given” error

today im trying to sort array by passing a variable into its function. I can print and interact with the array just fine, but for some reason i cant pass it to a function. I've tried printing the ...
0
votes
2answers
40 views

Sorting a PHP Object array without keys?

I am generating a list of cuisines like this: $lists=''; $stmt="SELECT cuisine_id, cuisine_name, cuisine_name_trans FROM db_cuisine"; if ( $res=$DbExt->rst($stmt)){ if ($list){ ...
1
vote
1answer
43 views

usort array by two parameters using spaceship operator [duplicate]

Is there a more compact way to sort an array by two parameters/fields with PHP ≥7.0 (using the spaceship operator <=>) ? Right now I just to the trick to sort is first by the second parameter ...
2
votes
1answer
32 views

Usort being skipped unless watched

This is a strange situation, I have a usort function that seems to work fine only if I am monitoring code execution through xdebug. No clue why. It's almost as if the usort call is being skipped ...
0
votes
0answers
33 views

PHP sort with custom function using additional data [duplicate]

How to sort an array using a custom sort function which requires additional data, in addition to the two objects being compared? i.e. a structure something like this: myfunction($item1, $item2, $...
0
votes
2answers
63 views

PHP usort for string hierarchy

I have a array with a string hierarchy like so: table, parent_table test, NULL test, NULL test2, test test4, NULL test5, test3 test6, test5 test3, test I want to sort with a function that goes ...
1
vote
1answer
44 views

Custom sort a multidimensional array by subarray keys using another array

I have 2 arrays. Array1 looks something like this (which is just a small example, in reality it haves more then 408 rows): Array ( [0] => Array ( [536870925] => 34213897 ) [1] => ...
-1
votes
1answer
43 views

How can I sort an object in symfony?

This is "fields" {#751 ▼ +"id": array:9 [▼ "fieldName" => "id" "type" => "integer" "scale" => 0 "length" => null "unique" => true "nullable" => false "...
1
vote
1answer
55 views

Sort and filter based on multiple criteria from REST API call

I would like to sort the data I receive from an API call on multiple criteria and afterwards filter it to display the data in 2 different tables. This is the code that I have now to sort the data, on ...
-3
votes
3answers
110 views

Is this a array or not?

I have this array filled with data from a database $collectTable1 = array( 'errand' => $interest->errand_id, 'timestamp' => $interest->timestamp, ...
0
votes
1answer
40 views

Sort array by property in another array

I have two arrays of the same size. One contains all product information and the other one contains only product_id and position. I want to order the first array by the order specified in the second ...
0
votes
0answers
21 views

How can I create a usort function php to list array an compare arrays

I am still learning php I would like to discover how can I create a function compare and list using usort() for this array and debug render for phpunit? // create an array with names to be sorted $...
-1
votes
1answer
46 views

How to sort multidimensional array by highest value using php usort? [duplicate]

How can i sort my multidimensional array based on inner array value descending? My array is: array [ 0 => array [ "name_TH" => "test" "currnetMonth" => 200.0 ] 1 => array ...
0
votes
2answers
44 views

Sort multidimensional array by usort

I want to create universal function to sort multidimensional array. For example: I have this array $arr = [ [ 'product' => [ 'id' => 32, ], 'price' => ...
2
votes
1answer
46 views

usort not working for laravel multidimensional arrays

I have an array Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => stdClass Object ( [id] => 79 ...
1
vote
1answer
51 views

usort comparison function does not appear to be called

I have an array with objects and I want to sort the array by an object property. So I have to use usort for that. But my array keeps unsorted. So I put some var_dumps to my code to debug it. Here is ...
0
votes
4answers
408 views

PHP sort array keys by alphabetically and then numerically

I am trying to sort an array (with both alphabetic and numeric keys) by keys where the alphabetic keys will come first alphabetically and then numeric keys numerically. Tried ksort with all the ...
0
votes
2answers
45 views

PHP Sort by strnatcmp and by another specific keyword

I need to sort an multi dimensional array which looks like this down below (usort already applied). I need to move '10000_M3' to the first key if an identifier '_M' was found (as natural sorting), but ...
0
votes
0answers
30 views

Variable undefined when using it as array key [duplicate]

I am trying to sort an associative array based on value of specific key, which is supplied by parameter. However it says that variable is undefined, when it is passed as parameter. See this: function ...
0
votes
4answers
190 views

usort function result is wrong

Today I provided the answer of this question and I wrote a script, but I found out that something went wrong. Here is the first script <?php $array = array( "0" => array ( ...
0
votes
1answer
84 views

json array sort by value in php

I have JSON arrays of objects. I am trying to sort an array using usort. I want to use value field from field_listing_order. It sorted using value. I am missing something but not able to figure it out....
0
votes
1answer
20 views

Usort in Opencart

I'm trying to order an array like this in my opencart 3.0.2: array(4) { [0]=> array(3) { ["name"]=> string(5) "Name1" ["download_id"]=> int(1) ["event_date"]=&...
0
votes
2answers
95 views

Sort array by non-alphabetical, user defined string values

I want to sort an array in a complicated way and am not sure how to go about it. Here is a rough idea of the data I'm working with: [ { target: random.text.cpu-pct-0, otherData[...] }, { target: ...
1
vote
1answer
102 views

PHP : using Usort() inside a Loop

I am trying to sorting multidimensional Array. I have the following structure in my Array: array ( [1] => stdClass Object ( [events] => Array ( ...
0
votes
1answer
57 views

usort function not working as expected

I would like some help regarding my sort function for an array as it is not working as I expected. This function is to sort array of [0,1,2,3,4,...23] into array [9,10,11,12,...,23,0,1,2,3,4,5,6,7,8]...
-1
votes
4answers
42 views

How can i sort this multidimensional array?

i can't make the sorting on this multidimensional array, i need to sort from lower to Higher by "packagenumber" value This is my array: I'm trying with usort: uasort($data, function($a, $b) { ...
0
votes
1answer
101 views

Explaining usort() in detail

Hi i'm currently trying to understand how the function I created "cmp()" works along with the function usort(). I just can't seem to get the logic. Can anybody help me out? <?php function cmp($a, $...
0
votes
4answers
62 views

PHP usort Multi-dimensional array

I'm having trouble getting usort to work and not sure what I'm missing. Below is an example of my array. I want to sort the array based on the value of the sort key. Array ( [0] => Array ( ...
0
votes
1answer
195 views

Sorting an array of objects by alphabetical order of a property of an object in php?

Here is an example of what I want, I basically want my retailers object to be sorted alphabetically by the name of of each retailer but I am not sure of the correct syntax on how to achieve it: ...
4
votes
3answers
53 views

Usort sort multidimensional array by continuing values

I have an array: $cards = [ [ "from" => "Barcelona", "to" => "Gerona Airport", ], [ "from" => "Stockholm", "to" => "New York JFK", ...
0
votes
3answers
392 views

Using usort results in an error when trying to sort array of objects

Please explain why I receive this error when trying to sort array of objects: usort() expects parameter 1 to be array, object given Screenshot of array: http://prntscr.com/ik3ira Screenshot of error: ...
0
votes
2answers
25 views

PHP usort() not accurate?

I have a multi-dimensional array. Inside each array, their is a sub value called "elo-rating". I want to sort my array based on this sub value. So I take my array, called $newRanks and run it through ...
0
votes
1answer
27 views

Sorting strings in format HH:mm in array with PHP

I have an Array like this one: $x = array( array( 'inicio' => '09:00', 'fim' => '09:30' ), array( 'inicio' => '09:30', 'fim' => '10:00' ), ...
0
votes
1answer
44 views

Grouping with usort function

I need to sort tags. Each tag looks like this "Firstname Lastname (University)". I want the names to be sorted by university, then echoed like this: University1: Firstname1 Lastname1 (University1) ...
0
votes
2answers
21 views

Multisort by distance

I have a multidimensional array and want to sort it by distance: [clustermarkers] => [ 0 => [ 0 => [ 'name' => 'A', ...
-1
votes
1answer
40 views

Array being mysteriously converted to object immediately before usort() sees it

I'm getting a good laugh out of this situation. It's in a giant application, so I can't just upload everything, but here is the local, relevant part of my code: ``` public function reallyCustomSort($...
0
votes
2answers
82 views

Can I use usort to sort primary by 1 value and secondary by second value?

So this function works great for sorting array or object by 1 value function sort( $a, $b ) { if( $a['1'] == $b['1'] ){ return 0 ; } return ($a[...
0
votes
1answer
48 views

usort table by date given by XML

My problem is, that I change my date from valuable $date into an array, which is called $sortDate Now, I want to take the array, and sort it by date. My function for sorting looks like this. ...
-3
votes
1answer
54 views

PHP - How to order an array of objects using a defined getter on each element? [duplicate]

how we can order an array of objects by the returned value of a specified getter ? For example, We have an array of objects. Each object has a 'getCreatedAt' function, that returns a DateTime ...
1
vote
0answers
102 views

Sorting 3 or more values with tiebreakers in PHP

I'm ranking a list based on a set of tie-breaking criteria using usort in PHP. My problem is that the tiebreakers aren't applied correctly when there are 3 or more ties to break. The function only ...
0
votes
0answers
76 views

Does returning “-1” with usort really move the $b variable or does it keep it in the same place?

A simple piece of code written by me: <?php function testing($a,$b){ if ($a < $b ){ return -1; } elseif ($a > $b){ return 1; } //else { //return ...
0
votes
2answers
80 views

What is the reason of returning -1 instead of lets say 0 at the end of this function's code?

I am talking about the second "return -1;" on the 12th line of the code. This gets reached only if two sets of numbers are exactly the same, like when comparing '192.167.11' to '192.167.11'. I will ...
0
votes
2answers
39 views

ordering strings and numbers in php [duplicate]

I was experimenting with PHP type juggling and found a weired behavior I can't explain. I was sorting objects based on a property that is sometimes a string, sometimes a number. Using usort, some ...
-1
votes
2answers
51 views

What is the mechanism of ordering this array with usort()? [duplicate]

<?php function list_cmp($a, $b) { global $order; foreach ($order as $key => $value) { if ($a == $value) { return 0; } if ($b == $value) { ...
0
votes
3answers
64 views

Group Multi-dimensional Array by Value(a) and sort each Group by Value(b)

I have an array containing fruits and vegetables that are all mixed together. I would like to group each item by their type ie 'vegetable' (indicated by value = 1) or 'fruit'(indicated by value = 2) ...
4
votes
1answer
527 views

How the usort() sorting algorithm works?

I have an usort() example and I added some echo statements to see how the code works: <?php function list_cmp($a, $b) { global $order; echo "\$a=$a, \$b=$b </br>"; foreach ($...

http://mssss.yulina-kosm.ru