public function testSearchRecursive() { $array = array("key1" => "val1", array("key2" => "val2", array("key3" => "val3"))); $this->assertEquals(array("key1"), $this->stub->searchRecursive("val1", $array)); $this->assertEquals(array(array(array("key3"))), $this->stub->searchRecursive("val3", $array)); }
/** * A recursive version of array_search (works for multidimensional array). * The result is an array reproducing the structure of the haystack * * @param mixed $needle The needle * @param array $haystack The haystack * * @return array */ static function searchRecursive($needle, $haystack) { $path = array(); foreach ($haystack as $id => $val) { if ($val === $needle) { $path[] = $id; break; } elseif (is_array($val)) { $found = CMbArray::searchRecursive($needle, $val); if (count($found) > 0) { $path[$id] = $found; break; } } } return $path; }
/** * Sort the constants by rank, and create a structure for dividing the constants in graphs * * @param CConstantesMedicales[] $constants The CConstantesMedicales objects who contain the values for the constants * * @return array */ function sortConstantsbyGraph($constants) { $constants_list = array(); $constants_by_graph = array(); $constants_by_rank = CConstantesMedicales::getConstantsByRank('graph', false, $this->host); /* The valued constants are sorted by rank */ foreach (CConstantesMedicales::$list_constantes as $cst_name => $cst_attr) { if (substr($cst_name, 0, 1) == '_' && !isset($cst_attr['plot'])) { continue; } foreach ($constants as $cst) { if (!is_null($cst->{$cst_name}) && array_search($cst_name, $constants_list) === false) { $rank = CMbArray::searchRecursive($cst_name, $constants_by_rank); if (empty($rank)) { continue; } $rank = array_keys($rank['all']); $rank = $rank[0]; if (!array_key_exists($rank, $constants_by_graph)) { $constants_by_graph[$rank] = array(); } $constants_by_graph[$rank][] = $cst_name; $constants_list[] = $cst_name; } } } /* We remove the constant with the rank 0, sort the array and add the rank 0 at the end of the array */ $hidden_cst = null; if (array_key_exists('hidden', $constants_by_graph)) { $hidden_cst = $constants_by_graph['hidden']; unset($constants_by_graph['hidden']); } ksort($constants_by_graph); if (!is_null($hidden_cst)) { $constants_by_graph['hidden'] = $hidden_cst; } $stacked_graphs = CConstantesMedicales::getHostConfig('stacked_graphs', $this->host); foreach ($constants_by_graph as $_rank => $_constants) { $constants_by_graph[$_rank] = array(); if ($_rank != 'hidden' && $stacked_graphs) { $cumuls_constants = array(); foreach ($_constants as $_key => $_constant) { /* The constants with a cumul can't be stacked with other constants */ if (isset(CConstantesMedicales::$list_constantes[$_constant]['cumul_reset_config'])) { unset($_constants[$_key]); $cumuls_constants[] = $_constant; } } /* The number of constants by graph is limited to 5 */ if (count($_constants) > 5) { $constants = array(); for ($i = 0; $i < count($_constants); $i = $i + 5) { $constants[] = array_slice($_constants, $i, 5); } foreach ($cumuls_constants as $_key => $_cumul) { $cumuls_constants[$_key] = array($_cumul); } $constants_by_graph[$_rank] = array_merge($constants, $cumuls_constants); } else { if (!empty($_constants)) { $constants_by_graph[$_rank][] = $_constants; } foreach ($cumuls_constants as $_cumul) { $constants_by_graph[$_rank][] = array($_cumul); } } } else { /* The constants with a rank of 0 can't be stacked */ foreach ($_constants as $_constant) { $constants_by_graph[$_rank][] = array($_constant); } } } $this->structure = $constants_by_graph; }