0

I'm studying how to manipulate array in PHP. Now, I have a issue about multidimensional array. I want to reverse 2d array without any sort method.This issue doesn't allow us to use array_reverse.

$numArray = array(1,2,3,array(4,5,6),7);

function numReverse($num) {
$reverseArray=array();
$subValueArr=array();

for($count=count($num), $i=$count-1; $i >= 0; $i--){
$reverseArray[]=$num[$i];
}
foreach($num as $value){
If(is_array($value)){
for($subCount=count($value),$j=$subCount-1; $j--){
$subValueArr[]=$value[$j];
}
}
}
$reverseArray[1][]=$subValueArr;
return $reverseArray;
}

$result=numReverse($numArray);
print_r($result);

I expect this output

array{int(7),array{int(6),int(5),int(4)},int(3),int(2),int(1)}
5
  • what is your required output? Commented Oct 14, 2015 at 12:19
  • Post your expected output Commented Oct 14, 2015 at 12:21
  • I want like this. array{int(7),array{int(6),int(5),int(4)},int(3),int(2),int(1)} Commented Oct 14, 2015 at 12:22
  • Use reversed bubble sort algorithm to do this. Commented Oct 14, 2015 at 12:24
  • Just access reverse.... Suppose if you want to access array dynamically. For eg. if you want to access 30th element. Then in reverse it is at 70th position. then to get position... count(array_size)-(original array position). Commented Oct 14, 2015 at 12:30

3 Answers 3

2

Just do it as , (if you are rigid to not use array_reverse)

<?php
$numArray = array(1,2,3,array(4,5,6),7);
$result=reverse($numArray);
print_r($result);
function reverse($arr)
{
end($arr);
 do
 {
     if(is_array(current($arr)))
         $result[]=reverse(current($arr));
     else
         $result[]=current($arr);
 }while(prev($arr));
return $result;
}  
?>

OUTPUT

Array ( [0] => 7 [1] => Array ( [0] => 6 [1] => 5 [2] => 4 ) [2] => 3 [3] => 2 [4] => 1 ) 
Sign up to request clarification or add additional context in comments.

Comments

1

Easiest way is to use array_reverse (for your 2d array):

$numArray = array(1,2,3,array(4,5,6),7);
foreach ($numArray as &$ar) {
    if (is_array($ar))
        $ar = array_reverse($ar);
}
$result = array_reverse($numArray);
var_dump($result);

For 3D and more dimesnions arrays you'll require a recursive function.

1 Comment

Can you explain &$ar in your foreach?
0

Using array_walk you'll walk through each array's element passing a custom callback (in this case, checking if they contain an array). If it does, the value receives the result of array_reverse, which gives the array in reverse order (set true after the value if you want to keep the key association)

<?php
$numArray = array(1,2,3,array(4,5,6),7);
array_walk($numArray, function(&$v) {
    return !is_array($v) ? : $v = array_reverse($v);
});

Outputs (before and after array_walk)

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => Array
        (
            [0] => 4
            [1] => 5
            [2] => 6
        )

    [4] => 7
)
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => Array
        (
            [0] => 6
            [1] => 5
            [2] => 4
        )

    [4] => 7
)

1 Comment

And reverse the result)

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.