Wild Widow
Wild Widow

Reputation: 2549

modifying multi dimensional array in PHP

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 !!!

Upvotes: 1

Views: 70

Answers (3)

Rasclatt
Rasclatt

Reputation: 12505

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
        )

)

Upvotes: 2

Girish
Girish

Reputation: 12117

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

Upvotes: 1

Quan Nguyen
Quan Nguyen

Reputation: 582

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/

Upvotes: 1

Related Questions