57636

I try to sort an array using sort() but it fails

if ( $_GET['_value'] == 'moto' ) { $array[] = array('1' => 'Yamaha'); $array[] = array('2' => 'Suzuki'); $array[] = array('3' => 'Triumph'); $array[] = array('4' => 'KTM'); $array[] = array('5' => 'Honda'); $array[] = array('6' => 'Harley Davidson'); $array[] = array('7' => 'Buell'); $array[] = array('8' => 'MV Agusta'); $array[] = array('9' => 'Ducati'); $array[] = array('10' => 'Other'); } $array = sort($array); echo json_encode( $array );

that is the code i have and its pulled by a chained dropdown. I want it to return the values sorted alphabetically but based on the code you see it returns an empty array. what could be the mistake I am making /

Answer1:

It's not at all pretty but this does the job.

Unless you're constrained otherwise you should really use some of the other suggestions.

<? if ( $_GET['_value'] == 'moto' ) { $array[] = array('1' => 'Yamaha'); $array[] = array('2' => 'Suzuki'); $array[] = array('3' => 'Triumph'); $array[] = array('4' => 'KTM'); $array[] = array('5' => 'Honda'); $array[] = array('6' => 'Harley Davidson'); $array[] = array('7' => 'Buell'); $array[] = array('8' => 'MV Agusta'); $array[] = array('9' => 'Ducati'); $array[] = array('10' => 'Other'); foreach($array as $i => $v) { $v = array_values($v); $sort[] = $v[0]; } sort($sort); $c = 0; foreach($sort as $i => $v) { $c++; $sorted[] = array($c=>$v); } echo json_encode($sorted); } ?>

Answer2:

Your code fails because you have an array of arrays here. You should either search for "sort php array by sub-array key" Or you can try something like:

$array[1] = 'Yamaha'; $array[2] = 'Suzuki'; // ... sort($array); echo json_encode($array);

Answer3:

Your array contains arrays, hence cannot be sorted, try:

$array[1] = 'Yamaha'; $array[2] = 'Suzuki';

then sort($array)

Answer4:

You can use uasort() function

like:

function cmp($a, $b) { $a = reset($a); $b = reset($b); if ($a == $b) { return 0; } return ($a < $b) ? -1 : 1; } uasort($array, 'cmp')

Recommend

  • Ajax Request always fire error even when json_encode is right and everything work as it should
  • jQuery autocomplete json source - Does not autocomplete, but just show the full list
  • Getting JSON info from PHP to android studio
  • How to encode 2 dimentional PHP array to Javascript Array?
  • Trying to understand array_udiff behavior
  • How to JSON data using $.getJSON from a php file?
  • Displaying JSON Data in my Twig template in symfony2
  • Using for loop to move files from subdirectories to parent directories
  • Handling base64 encoded images with json_encode and PHP
  • 'file_get_contents' The contents are encrypted?
  • json_encode returns 200 and undefined
  • What should I return in response to show error message on delete event for blueimp/jquery.file-uploa
  • json array for events array in jQuery fullcalendar
  • How to read CSV file and assign to Eigen Matrix?
  • How to return an object in my case?
  • SimpleXML - echo / print_r return different values
  • How can I pass a PHP variable to javascript?
  • How to move a text file using Oracle
  • copy an attribute of a property from one instance to another instance (of a different type ) at runt
  • Jquery Ajax call not executed in for loop
  • Need tricky idea to use JS variable in php
  • Stored data can't access in Yii
  • Is there a MySQL query that can encode to JSON in exactly this way?
  • FCM on app closed
  • Cannot use a lambda expression as an argument to a dynamically dispatched operation
  • Client side validation mvc dropdown
  • Creating a DropDownList
  • Can't remove headers after they are sent
  • ListItem.Attributes.Add not working
  • Stop Bash Script if Hive Fails
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • How can I sort a a table with VBA with given text condition?
  • Is it possible to access block's scope in method?
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • json Serialization in asp
  • Does armcc optimizes non-volatile variables with -O0?
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • How to push additional view controllers onto NavigationController but keep the TabBar?