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); } }
6
  • 1
    Please show us your progress so far. Commented Dec 6, 2017 at 4:02
  • Spent good amount of time , not able to figure it out. I am still trying Commented Dec 6, 2017 at 4:24
  • I'm quite sure Scuzzy wants you to show the code you have tried. Not just saying that you have tried. Commented Dec 6, 2017 at 4:26
  • Partial code so far.. Commented Dec 6, 2017 at 4: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); } } Commented Dec 6, 2017 at 4:33

2 Answers 2

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

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

7 Comments

Thank you very much. I am using php version 5.4 , would the above code change for old version?
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)));
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 check it now.
Works great. Wonderful. Thank you.
|
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!

3 Comments

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)));
according to your structure of array it will work definately :) @subra
$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

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.