1

How do I solve the following problem using PHP RecursiveIteratorIterator?

$arr = array(
    "sum"=>array(2,4,6, "multiply" => array(1,3,5) ),
    "multiply"=>array(3,3,3, "sum" => array(2,4,6) ),
);

I am expecting the following answers

(2 + 4 + 6 + ( 1 * 3 * 5) ) = 27; 
(3 * 3 * 3 * (2 + 4 + 6)) = 324;

Partial code so far..

   $calc = new ArrayObject($arr);
   $MRI = new RecursiveIteratorIterator(new      MyRecursiveIterator($calc), 1);
   foreach ($MRI as $key=>$value) {
   echo " Current Depth: ". $MRI->getDepth() . "\n"; 
   echo $key . " : " . $value . "\n";
   $currentDepth = $MRI->getDepth();
   for ($subDepth = $currentDepth; $subDepth >= 0; $subDepth--)
   { echo "sub Depth: ". $subDepth . "\n"; 
   $subIterator = $MRI->getSubIterator($subDepth);
   // var_dump($subIterator); } }
subra
  • 35
  • 6
  • 1
    Please show us your progress so far. – Scuzzy Dec 06 '17 at 04:02
  • Spent good amount of time , not able to figure it out. I am still trying – subra Dec 06 '17 at 04:24
  • I'm quite sure Scuzzy wants you to show the code you have tried. Not just saying that you have tried. – Andreas Dec 06 '17 at 04:26
  • Partial code so far.. – subra Dec 06 '17 at 04:31
  • Partial code.. $calc = new ArrayObject($arr); $MRI = new RecursiveIteratorIterator(new MyRecursiveIterator($calc), 1); foreach ($MRI as $key=>$value) { echo " Current Depth: ". $MRI->getDepth() . "\n"; ; echo $key . " : " . $value . "\n"; $currentDepth = $MRI->getDepth(); for ($subDepth = $currentDepth; $subDepth >= 0; $subDepth--) { echo "sub Depth: ". $subDepth . "\n"; ; $subIterator = $MRI->getSubIterator($subDepth); // var_dump($subIterator); } } – subra Dec 06 '17 at 04:33
  • Update the above code in the question. – Shihas Dec 06 '17 at 05:02

2 Answers2

1

You can do it like this, do the calculation from the innermost of the array. Check the demo.

<?php
function f(&$array)
{
    foreach($array as $k => &$v)
    {
        if(is_array($v))
        {
            if(count($v) == count($v, 1))
            {
                unset($array[$k]);
                if($k == 'sum')
                {
                    $v =  array_sum($v);
                    $array[] = $v;

                }elseif($k == 'multiply'){
                    $v = array_product($v);
                    $array[] = $v;
                }else{

                    foreach($v as $vv)
                        $array[] = $vv;
                }
            }else
                f($v);
        }
    }
}

while(count($array) != count($array, 1))
{
    f($array);
}

print_r($array);

Note:

traverse array from outer to inner
traverse array from inner to outer

LF00
  • 27,015
  • 29
  • 156
  • 295
  • Thank you very much. I am using php version 5.4 , would the above code change for old version? – subra Dec 06 '17 at 04:46
  • Also, will this work for deeper level of arrays? For example: $arr = array("sum"=>array(2,4,6, "multiply" => array(1,3, "sum" => array(1,5,6,"multiply"=>array(2,2,2, "sum" => array(4,4,4))))),"multiply"=>array(3,3,3, "sum" => array(2,4,6))); – subra Dec 06 '17 at 05:13
  • Kris, Evaluation stops at the most deepest level. I tried with the following array, it does not work. $arr = array( "sum"=>array(2,4,6, "multiply" => array(1,3,5, "sum" =>array(1,2)) ), "multiply"=>array(3,3,3, "sum" => array(2,4,6) ), ); Do you have any suggestions – subra Dec 07 '17 at 00:05
  • @subra check it now. – LF00 Dec 07 '17 at 01:54
  • Works great. Wonderful. Thank you. – subra Dec 07 '17 at 03:08
  • Kris, I had a situation where inner array has numeric key. I wonder how to approach the problem in this scenario? $arr = array( "sum"=>array(2,4,6, 3 => array("multiply" => array(1,3,5)) , "multiply"=>array(3,3,3, 3 => array("sum" => array(2,4,6)) ), ); – subra Dec 12 '17 at 15:30
  • That is simply amazing. – subra Dec 13 '17 at 12:47
0

Simple and easy solution :-

$arr = array("sum"=>array(2,4,6, "multiply" => array(1,3,5) ),"multiply"=>array(3,3,3, "sum" => array(2,4,6)));
    foreach($arr as $key => $newarr){
        if($key =="sum"){
            $sum = array_sum($newarr);
            $product = array_product($newarr['multiply']);
            $finalarray[$key] = $sum+$product;
        }elseif($key =="multiply") {
            $product = array_product($newarr);
            $sum = array_sum($newarr['sum']);
            $finalarray[$key] = $sum*$product;
        }
    }
    echo "<pre>"; print_r($finalarray);

Hope it helps!

kunal
  • 4,122
  • 12
  • 40
  • 75
  • Thank you kunal. It works. Just curious, would it work deeper levels of arrays ? for example, $arr = array("sum"=>array(2,4,6, "multiply" => array(1,3, "sum" => array(1,5)) ),"multiply"=>array(3,3,3, "sum" => array(2,4,6))); – subra Dec 06 '17 at 04:57
  • according to your structure of array it will work definately :) @subra – kunal Dec 06 '17 at 05:00
  • $arr = array("sum"=>array(2,4,6, "multiply" => array(1,3, "sum" => array(1,5,6,"multiply"=>array(2,2,2, "sum" => array(4,4,4))))),"multiply"=>array(3,3,3, "sum" => array(2,4,6))); I tried with this array. It does not work. May be I need to use recursive loops – subra Dec 06 '17 at 16:39