0

I am trying to sort an array by its value:

My array:

<pre>Array
(
   [1] => 11250
   [2] => 33750
   [3] => 56250
   [4] => 78750
   [5] => 101250
   [6] => 123750
   [7] => 146250
   [1.5] => 22500
   [2.5] => 45000
   [3.5] => 67500
   [4.5] => 90000
   [5.5] => 112500
   [6.5] => 135000
)

i want to reform it and it should be like:

<pre>Array
(
   [1] => 11250
   [1.5] => 22500
   [2] => 33750
   [2.5] => 45000
   [3] => 56250
   [3.5] => 67500
   [4] => 78750
   [4.5] => 90000
   [5] => 101250
   [5.5] => 112500
   [6] => 123750
   [6.5] => 135000
   [7] => 146250
)

I have tried ksort PHP function but it return only 1.I have tried other PHP funcions too but it is not working. can any one guide me how can i achive it please?

Ashish Patel
  • 3,551
  • 1
  • 15
  • 31
  • What exactly are your keys? Floats? Strings? What exactly does "return only 1" mean? – deceze Feb 25 '17 at 10:36
  • my arry keys are floating numbers and decimal numbers only. and i am trying to sort array in ajax. as ksort function is not working in ajax. – Ashish Patel Feb 25 '17 at 10:42
  • this question is not duplicate and i have gone through other posts of SO and not solving my problem.. – Ashish Patel Feb 25 '17 at 11:04
  • Please show a more complete sample which illustrates the problem. It's unclear what exactly you're doing and what exact output you get. – deceze Feb 25 '17 at 13:08

3 Answers3

2

ksort is OK

<?php
$arr=array ("1" => 11250,"2" => 33750, "1.5" => 22500, "2.5" => 45000);
ksort($arr);
print_r($arr);

Demo - https://eval.in/742712

P.S. I've an idea that you try

$arr=ksort($arr);

This function is boolean so it returns true/false, not the array;

Alexey Shatrov
  • 450
  • 4
  • 12
0

Use

ksort( $array, SORT_NUMERIC );  
keyBeatz
  • 139
  • 6
-1

For sorting array using key you can use PHP function ksort()

Maulik Savaliya
  • 1,262
  • 7
  • 17