user2536914
user2536914

Reputation: 45

How to sort an array in PHP in a specific way

Code:

$array = array('even' => array(array('key' => 'test','key2' => 'Wn'), array('key' => 'test3', 'key2' => 'Bx')),
                'not_even' => array(array('key' => 'test2','key2' => 'Xn'), array('key' => 'test4', 'key2' => 'Gy')),
);

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

Output:

Array
(
    [even] => Array
        (
            [0] => Array
                (
                    [key] => test
                    [key2] => Wn
                )

            [1] => Array
                (
                    [key] => test3
                    [key2] => Bx
                )

        )

    [not_even] => Array
        (
            [0] => Array
                (
                    [key] => test2
                    [key2] => Xn
                )

            [1] => Array
                (
                    [key] => test4
                    [key2] => Gy
                )

        )

)

I want sort it and result should be:

Array
(
    [0] => Array
        (
            [key] => test
            [key2] => Wn
        )

    [1] => Array
        (
            [key] => test2
            [key2] => Xn
        )

    [2] => Array
        (
            [key] => test3
            [key2] => Bx
        )

    [3] => Array
        (
            [key] => test4
            [key2] => Gy
        )

)

So how sort it? Keys in arrays should be: test test2 test3 test4. How use foreach or something else for this? What is the best solution. Important is "key", "key2" is not matter.

Upvotes: 0

Views: 67

Answers (3)

RaMeSh
RaMeSh

Reputation: 3424

Try this example and i have tested in my system and it's working perfectly.

<?php
$array = array('even' => array(array('key' => 'test','key2' => 'Wn'), array('key' => 'test3', 'key2' => 'Bx')),
                 'not_even' => array(array('key' => 'test2','key2' => 'Xn'), array('key' => 'test4', 'key2' => 'Gy')),
);
//Before Sorting
echo '<pre>';
print_r($array);
echo '</pre>';

$my_array = array();
foreach($array as $values){
$my_array = array_merge($my_array, $values);
}

//After Sorting
asort($my_array);
echo '<pre>';
print_r($my_array );
echo '</pre>'; 
?> 

OutPut:-

Array
(
    [even] => Array
        (
            [0] => Array
                (
                    [key] => test
                    [key2] => Wn
                )

            [1] => Array
                (
                    [key] => test3
                    [key2] => Bx
                )

        )

    [not_even] => Array
        (
            [0] => Array
                (
                    [key] => test2
                    [key2] => Xn
                )

            [1] => Array
                (
                    [key] => test4
                    [key2] => Gy
                )

        )

)

Array
(
    [0] => Array
        (
            [key] => test
            [key2] => Wn
        )

    [2] => Array
        (
            [key] => test2
            [key2] => Xn
        )

    [1] => Array
        (
            [key] => test3
            [key2] => Bx
        )

    [3] => Array
        (
            [key] => test4
            [key2] => Gy
        )

)

I think this is want you need.

Upvotes: 0

Vazgen Manukyan
Vazgen Manukyan

Reputation: 1520

try this

$array = array('even' => array(array('key' => 'test5','key2' => 'Wn'),    array('key' => 'test10', 'key2' => 'Bx')),
'not_even' => array(array('key' => 'test1','key2' => 'Xn'), array('key' => 'test', 'key2' => 'Gy')),
);

$new_array = array();

// changing  structure
foreach($array as $values){
$new_array = array_merge($new_array, $values);
}

// dump array
echo '<pre>';
print_r($new_array );
echo '</pre>';

// sorting
/// function for sorting
function cmp($a, $b)
{
   return strcasecmp($a['key'], $b['key']);
}

// sort by 'key' 
uksort($new_array, "cmp");

// dump array
echo '<pre>';
print_r($new_array );
echo '</pre>';

Note: this is string sorting so test10 < test5

Upvotes: 1

Ahmed Ziani
Ahmed Ziani

Reputation: 1226

You can use array_multisort()

Try something like this:

foreach ($array as $key => $row) {
   $array1[$key]  = $row[0]; 
}

array_multisort($array1, SORT_DESC, $array);

Upvotes: 0

Related Questions