phpMax
phpMax

Reputation: 149

How to sort PHP array when there are multiple values

I have an array as below:

$people = array(
    200 => array(
        'id' => 12345,
        'first_name' => 'Joe',
        'surname' => 'Bloggs',
        'age' => 23,
        'sex' => 'm'
    ),
    100 => array(
        'id' => 12346,
        'first_name' => 'Adam',
        'surname' => 'Smith',
        'age' => 18,
        'sex' => 'm'
    ),
    500 => array(
        'id' => 12347,
        'first_name' => 'Amy',
        'surname' => 'Jones',
        'age' => 21,
        'sex' => 'f'
    )

I want to sort this array on the first value that you can see as 200, 100 and 500.

Upvotes: 0

Views: 92

Answers (5)

Danish Barry
Danish Barry

Reputation: 56

$result = array(
    array('name' => 'Tahir', 'age' => '40'),
    array('name' => 'Usman', 'age' => '30'),
    array('name' => 'Danish', 'age' => '20'),
    array('name' => 'Aneeq', 'age' => '10')
);

echo '<pre>';
print_r($result);
echo '</pre>';

foreach ($result as $key => $row) {
    $name[$key]  = $row['name'];
    $age[$key] = $row['age'];
}


array_multisort($age, SORT_ASC, $result);

echo '<pre>';
print_r($result);
echo '</pre>';

echo "<pre>";
echo "Name\t\tAge";
foreach ( $result as $var ) {
    echo "\n", $var['name'], "\t\t", $var['age'];
}

Upvotes: 2

Muhammad Umar
Muhammad Umar

Reputation: 348

You can use the following two functions according to your requirement:-

ksort() - sort associative arrays in ascending order, according to the key

krsort() - sort associative arrays in descending order, according to the key

How you can use it:-

ksort($people);

krsort($people);

Upvotes: 0

Will
Will

Reputation: 24699

This should be as simple as:

ksort($people);

To make it sort by the actual numeric value (to make 1000 come after 500 not after 100), use:

ksort($people, SORT_NUMERIC);

ksort() sorts an array by its keys.

For example:

php > var_dump($people);
array(3) {
  [200]=>
  array(5) {
    ["id"]=>
    int(12345)
    ["first_name"]=>
    string(3) "Joe"
    ["surname"]=>
    string(6) "Bloggs"
    ["age"]=>
    int(23)
    ["sex"]=>
    string(1) "m"
  }
  [100]=>
  array(5) {
    ["id"]=>
    int(12346)
    ["first_name"]=>
    string(4) "Adam"
    ["surname"]=>
    string(5) "Smith"
    ["age"]=>
    int(18)
    ["sex"]=>
    string(1) "m"
  }
  [500]=>
  array(5) {
    ["id"]=>
    int(12347)
    ["first_name"]=>
    string(3) "Amy"
    ["surname"]=>
    string(5) "Jones"
    ["age"]=>
    int(21)
    ["sex"]=>
    string(1) "f"
  }
}
php > ksort($people);
php > var_dump($people);
array(3) {
  [100]=>
  array(5) {
    ["id"]=>
    int(12346)
    ["first_name"]=>
    string(4) "Adam"
    ["surname"]=>
    string(5) "Smith"
    ["age"]=>
    int(18)
    ["sex"]=>
    string(1) "m"
  }
  [200]=>
  array(5) {
    ["id"]=>
    int(12345)
    ["first_name"]=>
    string(3) "Joe"
    ["surname"]=>
    string(6) "Bloggs"
    ["age"]=>
    int(23)
    ["sex"]=>
    string(1) "m"
  }
  [500]=>
  array(5) {
    ["id"]=>
    int(12347)
    ["first_name"]=>
    string(3) "Amy"
    ["surname"]=>
    string(5) "Jones"
    ["age"]=>
    int(21)
    ["sex"]=>
    string(1) "f"
  }
}
php >

Upvotes: 2

Makwana Ketan
Makwana Ketan

Reputation: 1388

You can try this solution.

<?php
  $people = array(
  200 => array(
    'id' => 12345,
    'first_name' => 'Joe',
    'surname' => 'Bloggs',
    'age' => 23,
    'sex' => 'm'
),
100 => array(
    'id' => 12346,
    'first_name' => 'Adam',
    'surname' => 'Smith',
    'age' => 18,
    'sex' => 'm'
),
500 => array(
    'id' => 12347,
    'first_name' => 'Amy',
    'surname' => 'Jones',
    'age' => 21,
    'sex' => 'f'
));
ksort($people, SORT_NUMERIC);
print_r($people);
?>

Upvotes: 0

Wasabi
Wasabi

Reputation: 63

You can use ksort() use ascending order and krsort() for descending order.

ksort($people);

krsort($people);

Upvotes: 2

Related Questions