7

This is my current Array

Array
(
    [0] => [10,13],
    [1] => [11,15],
    [2] => [12,7],
    [3] => [1,0],
    [4] => [2,0],
    [5] => [3,0],
    [6] => [4,0],
    [7] => [5,0],
    [8] => [6,0],
    [9] => [7,0],
    [10] => [8,0],
    [11] => [9,0]
);

I want to sort this array by the value(value before comma).
Result should be like this:

Array
(    
    [1] => [1,0],
    [2] => [2,0],
    [3] => [3,0],
    [4] => [4,0],
    [5] => [5,0],
    [6] => [6,0],
    [7] => [7,0],
    [8] => [8,0],
    [9] => [9,0],
    [10] => [10,13],
    [11] => [11,15],
    [12] => [12,7]
);

Is it possible in PHP ?

6
  • 1
    Its possible you need a loop that compares the value, this is quite simple. If you google this there should be TONS of answers for this .. Array sorting
    – I am Cavic
    Commented Dec 24, 2013 at 5:37
  • 1. usort 2. Does the second array have to start with index 1?
    – zerkms
    Commented Dec 24, 2013 at 5:38
  • @LJ-C: Well ! Thank you for your comment but if I searched it successfully why should I post here ? Commented Dec 24, 2013 at 5:39
  • 1
    Might be rhetorical, but the brackets are making me ask...are your array values dealing with other arrays (multidimensional arrays) or plain string values? Commented Dec 24, 2013 at 5:39
  • @Crackertastic: Thanks for comment. This is not a multidimensioal array it just a string. Commented Dec 24, 2013 at 5:41

9 Answers 9

6

OPTION 1 : IF TWO DIMENSIONAL ARRAY TRY THIS ONE

$data = array();
$data[0] = array(10, 13);
$data[2] = array(11, 15);
$data[3] = array(12, 7);
$data[4] = array(1, 0);
$data[5] = array(2, 0);
$data[6] = array(3, 0);
$data[7] = array(4, 0);
$data[8] = array(5, 0);
$data[9] = array(6, 0);
$data[10] = array(7, 0);
$data[11] = array(8, 0);
$data[12] = array(9, 0);


echo "<pre>";
//print_r($data);
array_multisort($data);
print_r($data);
echo "</pre>";
?>

OUTPUT

Array
(
[0] => Array
    (
        [0] => 1
        [1] => 0
    )

[1] => Array
    (
        [0] => 2
        [1] => 0
    )

[2] => Array
    (
        [0] => 3
        [1] => 0
    )

[3] => Array
    (
        [0] => 4
        [1] => 0
    )

[4] => Array
    (
        [0] => 5
        [1] => 0
    )

[5] => Array
    (
        [0] => 6
        [1] => 0
    )

[6] => Array
    (
        [0] => 7
        [1] => 0
    )

[7] => Array
    (
        [0] => 8
        [1] => 0
    )

[8] => Array
    (
        [0] => 9
        [1] => 0
    )

[9] => Array
    (
        [0] => 10
        [1] => 13
    )

[10] => Array
    (
        [0] => 11
        [1] => 15
    )

[11] => Array
    (
        [0] => 12
        [1] => 7
    )
)

OPTION 2 : IF ONE DIMENSIONAL ARRAY TRY THIS ONE

$data = array();
$data[0] = "10,13";
$data[2] = "11,15";
$data[3] = "12,7";
$data[4] = "1,0";
$data[5] = "2,0";
$data[6] = "3,0";
$data[7] = "4,0";
$data[8] = "5,0";
$data[9] = "6,0";
$data[10] = "7,0";
$data[11] = "8,0";
$data[12] = "9,0";

echo "<pre>";
//print_r($data);
echo "</pre>";

function dataSort($data){
    $data_temp =array();
    foreach($data as $item){
        $data_temp[] = explode(",",$item);
    }
    array_multisort($data_temp);

    $data =array();
    foreach($data_temp as $item){
        $data[] = $item[0].",".$item[1];
    }
    return $data;
}
$data  = dataSort($data); 
print_r($data);

OUTPUT

Array
(
   [0] => 1,0
   [1] => 2,0
   [2] => 3,0
   [3] => 4,0
   [4] => 5,0
   [5] => 6,0
   [6] => 7,0
   [7] => 8,0
   [8] => 9,0
   [9] => 10,13
   [10] => 11,15
   [11] => 12,7
)
2
  • It just change the first value Array ( [0] => [1,0], [1] => [10,13], [2] => [11,15], [3] => [12,7], [4] => [2,0], [5] => [3,0], [6] => [4,0], [7] => [5,0], [8] => [6,0], [9] => [7,0], [10] => [8,0], [11] => [9,0] ) Commented Dec 24, 2013 at 6:01
  • I have added two options, for single dimensional and two multi dimensional array.....
    – Sudheesh.R
    Commented Dec 24, 2013 at 6:09
3

Try this

function mSort($a,$b) {
    if ($a[0] == $b[0]) {
        return 0;
    }
    return ($a[0] < $b[0]) ? -1 : 1;
}

usort($arr, "mSort");

print_r($arr);
2

You may use usort() function or any of this list, what's near to you. For example:

<?php

$array = array (
    array(10,13),
    array(11,15),
    array(12,7),
    array(1,0),
    array(2,0),
    array(3,0),
    array(4,0),
    array(5,0),
    array(6,0),
    array(7,0),
    array(8,0),
    array(9,0)
);

function mySort( $l, $r ) {
   return (( $l[0] == $r[0] ) ? 0 : ($l[0] > $r[0] ? 1 : -1) );
}

usort( $array, 'mySort' );

print_r( $array );

?>

demo

1
  • I posted same answer before him :)
    – zzlalani
    Commented Dec 24, 2013 at 6:11
1

You can try

$new = array();
foreach($first_array as $key => $value) {
     $new[$key] = $value;
 }
array_multisort($new, SORT_ASC, $first_array);
1

This is What you want DEMO

 <?php


$yourArray = array(
    1 => array(
        10,
        13
    ),
    2 => array(
        11,
        15
    ),
    3 => array(
        12,
        7
    ),
    4 => array(
        1,
        0
    ),
    5 => array(
        2,
        0
    ),
    6 => array(
        3,
        0
    ),
    7 => array(
        4,
        0
    ),
    8 => array(
        5,
        0
    ),
    9 => array(
        6,
        0
    ),
    10 => array(
        7,
        0
    ),
    11 => array(
        8,
        0
    ),
    12 => array(
        9,
        0
    )
);

$array = array();

foreach ($yourArray as $key => $row) {
    $array[$key] = $yourArray['value'];
}
array_multisort($array, SORT_DESC, $yourArray);

$yourArray = array_combine(range(1, count($yourArray)), array_values($yourArray));

print_r($yourArray);

?> 
2
  • It just change the first value Array ( [0] => [1,0], [1] => [10,13], [2] => [11,15], [3] => [12,7], [4] => [2,0], [5] => [3,0], [6] => [4,0], [7] => [5,0], [8] => [6,0], [9] => [7,0], [10] => [8,0], [11] => [9,0], ) Commented Dec 24, 2013 at 6:00
  • you mean you want to remaining the key as your output ?
    – underscore
    Commented Dec 24, 2013 at 6:04
0

You need to access $value as a reference to the entry in $array:

private function orderByKey(&$array) {
    ksort($array);
    foreach($array as &$value) {
        if (is_array($value)) {
            $this->orderByKey($value);
        }
    }
}
0

You can use array_multisort() function for this

$arr = array([10,13],[11,15],[12,7],[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0],[8,0],[9,0]);

        array_multisort($arr);
        var_dump($arr);exit;
0

Since the values you are dealing with are strings (per your response to my comment) you need to isolate the first numerical value, then sort based on that. (Please note this does not support multiples of the same "first number").

Example:

$a = array("[1,10]", "[2,19]", "[6,13]", "[2,15]", "[4,16]");

$sortable = array();

foreach($a as $subject) {
    $start = strpos($subject, "[");
    $end = strpos($subject, ",");
    $ndx = substr($subject, $start, $end - $start);
    $sortable[$ndx] = $subject;
}

ksort($sortable);

var_dump($sortable);

var_dump will produce:

array(4) {
  ["[1"]=>
  string(6) "[1,10]"
  ["[2"]=>
  string(6) "[2,15]"
  ["[4"]=>
  string(6) "[4,16]"
  ["[6"]=>
  string(6) "[6,13]"
}
0

you need to use usort function :

$a = array([10,13],[11,15],[12,7],[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0],[8,0],[9,0]);
print_r($a);
usort($a, "cmp");
print_r($a);
?>

See Example Here : http://ideone.com/gv4k70

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.