I need a way to reorder my multidimensional arrays, is there any easy solution for this? I've been doing this with array_values for single arrays before, but it dosen't support multidimensional arrays.

My array looks likt this

Array
(
    [2] => Array
        (
            [text] => test
        )

    [5] => Array
        (
            [text] => test
        )

    [8] => Array
        (
            [text] => test
        )

)

I would like it to be

Array
(
    [0] => Array
        (
            [text] => test
        )

    [1] => Array
        (
            [text] => test
        )

    [2] => Array
        (
            [text] => test
        )

)
link|improve this question

0% accept rate
If I understand this correctly, you want to sort this by key, right? Not remove array keys? Or are you sorting by individual 'text' items? – pp19dd Mar 20 at 20:58
Possible repeat? How do you reindex an array in PHP – showerhead Mar 20 at 21:08
feedback

6 Answers

You could use usort with a custom callback that always returns 0, which will not reorder the array.

usort($array, function($a, $b) { return 0; });
link|improve this answer
feedback

array_values works fine for me, producing exactly the result you want.

Although it's a little hard to tell because all your array elements are identical, but make sure you're correctly assigning the return value of array_values to your new variable.

link|improve this answer
+1 array_values will work fine. – dfsq Mar 20 at 21:02
feedback

I think you need following:

$results = $your_array;
$tmp = array();
foreach ($result as $r)
{
  $tmp[]=$r;
}
$results= $tmp;

Good luck,

link|improve this answer
That's exactly array_values(). – lorenzo-s Mar 20 at 21:02
I like writing functions but you are right. – yahyaE Mar 20 at 21:07
feedback

Do a array_merge() on your array with a new array() and the keys will be reset

link|improve this answer
feedback

I'm afraid you are using array_values() the wrong way. Try:

$yourArray = array_values($yourArray);
link|improve this answer
feedback

Use ksort function

link|improve this answer
While this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. – Bill the Lizard Mar 22 at 12:08
feedback

Your Answer

 
or
required, but never shown

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