Google User
Google User

Reputation: 171

Array sort by First Value

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 ?

Upvotes: 7

Views: 4273

Answers (9)

Sudheesh.R
Sudheesh.R

Reputation: 356

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
)

Upvotes: 6

shifu
shifu

Reputation: 6816

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

Upvotes: 0

underscore
underscore

Reputation: 6887

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);

?> 

Upvotes: 1

Crackertastic
Crackertastic

Reputation: 4913

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]"
}

Upvotes: 0

theDilletante
theDilletante

Reputation: 98

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

Upvotes: 2

zzlalani
zzlalani

Reputation: 24354

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);

Upvotes: 3

Ajay Nigam
Ajay Nigam

Reputation: 36

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;

Upvotes: 0

Priya jain
Priya jain

Reputation: 703

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);
        }
    }
}

Upvotes: 0

shanks
shanks

Reputation: 2017

You can try

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

Upvotes: 1

Related Questions