1

I have this input array which is multi-dimensional.

$input = array(
    array(11, 12, 13, 14, 15),
    array(21, 22, 23, 24, 25),
    array(31, 32, 33, 34, 35),
    array(41, 42, 43, 44, 45),
    array(51, 52, 53, 54, 55)
);

I want to subtract 1 from each entity in this array, so that the result becomes

$output = array(
    array(10, 11, 12, 13, 14),
    array(20, 21, 22, 23, 24),
    array(30, 31, 32, 33, 34),
    array(40, 41, 42, 43, 44),
    array(50, 51, 52, 53, 54)
);

my solution

function sub ($a)
{
    return $a-1;  // what should go here? 
}
$output = array_map("sub", $input);

Please help !!!

3
  • Not expert with array_map here, I prefer to do the code with a foreach loop ... what do you think ? Commented Aug 19, 2015 at 3:53
  • I thought so, but the code will be big since you have to traverse through each element, array_map essentially does the same thing and lot cleaner. Commented Aug 19, 2015 at 3:55
  • Agree for array_map but the foreach loop way will not be so big ... Commented Aug 19, 2015 at 4:03

3 Answers 3

2

I'm sure someone smarter than me can think of another way around using a loop, but I can only think of using another array_map() if you didn't want a foreach():

<?php
    function subtractor($v)
        {
            $sub    =   1;
            return ((int)$v - (int)$sub);
        }

    function annon($v)
        {
            return array_map("subtractor",$v);  
        }

    $arr    =   array_map("annon", $input);

    print_r($arr);
?>

Gives you:

Array
(
    [0] => Array
        (
            [0] => 10
            [1] => 11
            [2] => 12
            [3] => 13
            [4] => 14
        )

    [1] => Array
        (
            [0] => 20
            [1] => 21
            [2] => 22
            [3] => 23
            [4] => 24
        )

    [2] => Array
        (
            [0] => 30
            [1] => 31
            [2] => 32
            [3] => 33
            [4] => 34
        )

    [3] => Array
        (
            [0] => 40
            [1] => 41
            [2] => 42
            [3] => 43
            [4] => 44
        )

    [4] => Array
        (
            [0] => 50
            [1] => 51
            [2] => 52
            [3] => 53
            [4] => 54
        )

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

Comments

1

array_map function is taking one dim array, you will need to perform twice, see code

<?php 
$input = array(
    array(11, 12, 13, 14, 15),
    array(21, 22, 23, 24, 25),
    array(31, 32, 33, 34, 35),
    array(41, 42, 43, 44, 45),
    array(51, 52, 53, 54, 55)
);
$op = array_map(function($v){
  return array_map(function($v1){return $v1 - 1;}, $v);
}, $input);
print_r($op);

DEMO

2 Comments

thanks for the solution, is it not possible by using array_map just once? maybe I can use an index or something in the callback function? like return $a[some index]-1 ;
well, overall thing the array will iterate to subtract each value even user array_map method or loop e.g.. foreach, if you want keep index then you can use foreach loop twice
1

Another way you can try

<!DOCTYPE html>
<html>
<body>

<?php
$cars = array(
    array(11, 12, 13, 14, 15),
    array(21, 22, 23, 24, 25),
    array(31, 32, 33, 34, 35),
    array(41, 42, 43, 44, 45),
    array(51, 52, 53, 54, 55)
);
     
for ($row = 0; $row <  5; $row++) {
   echo "<p><b>Row number $row</b></p>";
   echo "<ul>";
   for ($col = 0; $col <  5; $col++) {
	   $cars[$row][$col] = $cars[$row][$col] - 1;
     echo "<li>".$cars[$row][$col]."</li>";
   }
   echo "</ul>";
}
?>

</body>
</html>

Run code with http://phpfiddle.org/

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.