Jameswalker Posted June 6, 2018 Share Posted June 6, 2018 I'm having some trouble with an array I am ordering I've tried a few sorting options, the latest is usort. Currently, it's looking at the first character of each number and ordering based on that, rather than treating the numeric characters as a full number. I've tried adding substr but it hasn't made any difference... The array I'm getting back at the moment looks like this: [0] => Array ( [0] => 1 The Street, The City, The County [1] => FriA ) [1] => Array ( [0] => 10 The Street, The City, The County [1] => FriB ) [2] => Array ( [0] => 11 The Street, The City, The County [1] => FriA ) The first value is the address, the second is a schedule. 2 The Street is currently displaying after 19, rather than 1. Here is the code: function cmp( $a, $b ) { return strcmp( substr( $a[ '0' ], 0, 2 ), substr( $b[ '0' ], 0, 2 ) ); if ( $a == $b ) { return 0; } return ( $a < $b ) ? -1 : 1; } foreach ( $json[ 'candidates' ] as $value ) { $address = $json[ 'candidates' ][ $index ][ 'attributes' ][ 'ADDRESS_1' ]; $code = $json[ 'candidates' ][ $index ][ 'attributes' ][ 'CODE' ]; $a[] = array( $address, $code ); usort($a, "cmp"); $index++; } echo '<pre>';print_r( $a );echo '</pre>'; Any help would be greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/307350-help-with-sorting-an-array/ Share on other sites More sharing options...
Barand Posted June 6, 2018 Share Posted June 6, 2018 Firstly function cmp( $a, $b ) { return strcmp( substr( $a[ '0' ], 0, 2 ), substr( $b[ '0' ], 0, 2 ) ); if ( $a == $b ) { return 0; } return ( $a < $b ) ? -1 : 1; } The last 4 lines of that function are redundant and never executed. All you need is the "return strcmp(...)" Secondly, you should sort the array *after* you have built it. You are currently sorting inside the loop each time you add a new element. Quote Link to comment https://forums.phpfreaks.com/topic/307350-help-with-sorting-an-array/#findComment-1558787 Share on other sites More sharing options...
mac_gyver Posted June 6, 2018 Share Posted June 6, 2018 if you cast each string as an integer, any leading digits will be converted to their integer value. function cmp($a, $b) { $a = (int)$a; $b = (int)$b; if ($a == $b) { return 0; } return ($a < $b) ? -1 : 1; } Quote Link to comment https://forums.phpfreaks.com/topic/307350-help-with-sorting-an-array/#findComment-1558788 Share on other sites More sharing options...
Barand Posted June 6, 2018 Share Posted June 6, 2018 If you have cast them as integers then it's simpler than that function cmp($a, $b) { $a = (int)$a; $b = (int)$b; return $a - $b; } You just need to return -ve, 0 or +ve depending on a<b, a=b or a>b Quote Link to comment https://forums.phpfreaks.com/topic/307350-help-with-sorting-an-array/#findComment-1558789 Share on other sites More sharing options...
Jameswalker Posted June 6, 2018 Author Share Posted June 6, 2018 18 minutes ago, mac_gyver said: Thanks so much for your help guys, as you can probably tell I'm new at this! I've made the changes that you've suggested but now the order is randomised - I'm sure it's something I'm missing, so apologies in advance, I can tell it's pretty close... Quote Link to comment https://forums.phpfreaks.com/topic/307350-help-with-sorting-an-array/#findComment-1558790 Share on other sites More sharing options...
benanamen Posted June 6, 2018 Share Posted June 6, 2018 Were is this array coming from in the first place, a database? I smell an XY problem. Quote Link to comment https://forums.phpfreaks.com/topic/307350-help-with-sorting-an-array/#findComment-1558791 Share on other sites More sharing options...
Jameswalker Posted June 6, 2018 Author Share Posted June 6, 2018 Just now, benanamen said: Were is this array coming from in the first place, a database? I smell an XY problem. Hi, it's coming from an ESRI locatorhub REST API. Here's the latest code - function cmp($a, $b) { $a = (int)$a; $b = (int)$b; return $a - $b; } foreach ( $json[ 'candidates' ] as $value ) { $address = $json[ 'candidates' ][ $index ][ 'attributes' ][ 'ADDRESS_1' ]; $code = $json[ 'candidates' ][ $index ][ 'attributes' ][ 'CODE' ]; $a[] = array( $address, $code ); $index++; } usort($a, 'cmp'); echo '<pre>';print_r( $a );echo '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/307350-help-with-sorting-an-array/#findComment-1558792 Share on other sites More sharing options...
Barand Posted June 6, 2018 Share Posted June 6, 2018 Works for me. Have you checked your data? $arr = [ [ 0 => '1 The Street, The City, The County', 1 => 'FriA' ], [ 0 => '21 The Street, The City, The County', 1 => 'FriA' ], [ 0 => '10 The Street, The City, The County', 1 => 'FriB' ] ]; usort ($arr, function($a,$b) { return (int)$a[0] - (int)$b[0]; }); echo '<pre>', print_r($arr, 1), '</pre>'; /***** RESULTS ******/ Array ( [0] => Array ( [0] => 1 The Street, The City, The County [1] => FriA ) [1] => Array ( [0] => 10 The Street, The City, The County [1] => FriB ) [2] => Array ( [0] => 21 The Street, The City, The County [1] => FriA ) ) 1 Quote Link to comment https://forums.phpfreaks.com/topic/307350-help-with-sorting-an-array/#findComment-1558794 Share on other sites More sharing options...
Jameswalker Posted June 6, 2018 Author Share Posted June 6, 2018 6 minutes ago, Barand said: Works for me. Have you checked your data? $arr = [ [ 0 => '1 The Street, The City, The County', 1 => 'FriA' ], [ 0 => '21 The Street, The City, The County', 1 => 'FriA' ], [ 0 => '10 The Street, The City, The County', 1 => 'FriB' ] ]; usort ($arr, function($a,$b) { return (int)$a[0] - (int)$b[0]; }); echo '<pre>', print_r($arr, 1), '</pre>'; /***** RESULTS ******/ Array ( [0] => Array ( [0] => 1 The Street, The City, The County [1] => FriA ) [1] => Array ( [0] => 10 The Street, The City, The County [1] => FriB ) [2] => Array ( [0] => 21 The Street, The City, The County [1] => FriA ) ) Ok, now it's working. Just tweaked the structure to match your example and it's working like a dream. Thank you so so much Quote Link to comment https://forums.phpfreaks.com/topic/307350-help-with-sorting-an-array/#findComment-1558796 Share on other sites More sharing options...
Barand Posted June 6, 2018 Share Posted June 6, 2018 My structure is the same as the one you originally posted. Quote Link to comment https://forums.phpfreaks.com/topic/307350-help-with-sorting-an-array/#findComment-1558797 Share on other sites More sharing options...
benanamen Posted June 6, 2018 Share Posted June 6, 2018 Glad you got your code answer. Based on the Array you posted I am thinking you may not be using the API properly. Could you please post a link to the API Documentation. Quote Link to comment https://forums.phpfreaks.com/topic/307350-help-with-sorting-an-array/#findComment-1558799 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.