0

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.

1
  • You don't want to sort the array, you want to re-organize its data (and maybe sort it, it's not clear from the question). foreach() is a simple solution. The PHP functions array_map() and array_reduce() could also help. What have you tried? Commented Mar 27, 2015 at 10:52

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

it is not clear what by what you want to sort. From you code I see that structure of an array has been changed. But not sorted.
Your ansewer is good but it is not exactly what I want. The Result should be: $new_array[0][key] => test, $new_array[1][key] => test2 and so on, not $new_array[0][key] => test, $new_array[1][key] => test3.. so modulo or something like this on keys?
updated the code, now it is being sorted. You can also change the logic of "cmp" function.
0

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.

Comments

0

You can use array_multisort()

Try something like this:

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

array_multisort($array1, SORT_DESC, $array);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.