/** * Sums values in the pages/groups/files two arrays of N dimensions that can be very different * * @param array $a1 * @param array $a2 * * @return array */ function getPagGrpArrayMultiDimSum($a1, $a2) { if (!empty($a2) && is_array($a2)) { foreach ($a2 as $key => $value) { if (!isset($a1[$key])) { $a1[$key] = $value; } else { if (substr($key, 0, 1) === 'c') { $a1[$key] = getPagGrpArrayMultiDimSum($value, $a1[$key]); } else { $a1[$key] = sumArray($value, $a1[$key], true); } } } } return $a1; }
function test2(array $array) { return sumArray($array); }
<?php function testUnset($testArray) { if (array_key_exists('index2', $testArray) == TRUE) { //Could do sanity check on type here. unset($testArray['index2']); } return $testArray; } function sumArray($intArray) { $total = 0; foreach ($intArray as $value) { $total += $value; } return $total; } $array = array('index1' => 1, 'index2' => 2, 'index3' => 3); $unsetArray = testUnset($array); $result = sumArray($unsetArray); assert($result, 4); testEnd();
//************************************************************* //************************************************************* $testVar = 3; $stringArray = array('Hello', ' ', 'world'); $output = ''; foreach ($stringArray as $string) { $output .= $string; } assert($output, "Hello world"); //************************************************************* //************************************************************* $intArray = array(1, 1 => 2, 2 => 3, 'subArray' => array(1, 2, 3 => 3)); //$value = sumArray($intArray); $value = sumArray($intArray['subArray']); assert($value, 6); //************************************************************* //************************************************************* class ArrayTestClass { var $noteID = 123; function getTestArray() { $params = array('noteID' => $this->noteID); return $params; } } $arrayTestClass = new ArrayTestClass(); $testArray = $arrayTestClass->getTestArray(); $value = sumArray($testArray); assert($value, 123); testEnd();