/// it works like array_key_exists that can go deeper
function in_array_deeply($keys, $multi)
{
$depths = array();
$fun = null;
// inner, recursive function
function in_fun($level, $arr_keys, $subject, &$depths) {
// only if array
if (is_array($subject)) {
// if key exists
if (array_key_exists($arr_keys[$level], $subject)) {
// record depth and go deeper
$depths[] = $arr_keys[$level];
in_fun($level+1, $arr_keys, $subject[$arr_keys[$level]], $depths);
}
}
};
//
in_fun(0, $keys, $multi, $depths);
$srchd = implode('.',$keys);
$found = implode('.',$depths);
//
return ($srchd==$found);
}
/// example usage
$mapa = array('quadrada','comprida','amarela');
$cidade = array(
'redonda' => array(
'curta' => 'o seu filme'
),
'quadrada' => array(
'longa' => array(
'azul' => array('logo',2,'mais',2,'s?o',4),
'amarela' => array('ent?o',3,'vezes',2,'s?o',6),
'verde' => array('sen?o',100,'dividido por',2,'é',50)
),
'extravagante' => array(
'vermelha' => 'chama aten??o',
'vinho' => 'cor de uva',
'ocre' => 1255
),
'comprida' => array(
'amarela' => 'brasilia dos mamonas',
'branca' => 'bandeira da paz',
'preta e branca' => 'pe?as do xadrez'
)
),
'oval' => array(
'conde' => 'lobo'
),
'plana' => array(
'curta' => array(
'azul' => array('e',2,'mais',2,'s?o',4),
'amarela' => array('sim',3,'vezes',2,'s?o',6),
'verde' => array('verdade',100,'dividido por',2,'é',50)
)
)
);
$encontrou = in_array_deeply($mapa, $cidade);
echo $encontrou;
/// if the tree you search for exists, it will print out 'true'