search

Home  >  Q&A  >  body text

Php finds the sum of all odd items of the following two-dimensional array

$arr2=array(

Array(12,23,34,45),

Array (34,45,67,89),

Array( 56,78,90,1)

)

Is there anyone who knows?



PHP中文网用户-7954359PHP中文网用户-7954359450 days ago489

reply all(1)I'll reply

  • Ray

    Ray2023-10-18 17:00:18

    <?php

    $arr2 = array(
        array(12, 23, 34, 45),
        array(34, 45, 67, 89),
        array(56, 78, 90, 1)
    );
    $sum = 0;
    // 遍历
    foreach ($arr2 as $subArray) {
         // 遍历
        foreach ($subArray as $value) {
            // 判断奇数项
            if ($value % 2 != 0) {
                $sum += $value;
            }
        }
    }
    echo $sum;

    reply
    1
  • Cancelreply