note 125437 added to function.array-key-exists

From: Date: Tue, 13 Oct 2020 19:42:30 +0000
Subject: note 125437 added to function.array-key-exists
Groups: php.notes 
Request: Send a blank email to php-notes+get-215580@lists.php.net to get a copy of this message
/// 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' ---- Server IP: 45.112.84.4 Probable Submitter: 185.85.3.172 (proxied: 2804:d41:e02e:bb00:49cd:cad9:ef12:d559) ---- Manual Page -- http://php.net/manual/en/function.array-key-exists.php Edit -- https://master.php.net/note/edit/125437 Del: integrated -- https://master.php.net/note/delete/125437/integrated Del: useless -- https://master.php.net/note/delete/125437/useless Del: bad code -- https://master.php.net/note/delete/125437/bad+code Del: spam -- https://master.php.net/note/delete/125437/spam Del: non-english -- https://master.php.net/note/delete/125437/non-english Del: in docs -- https://master.php.net/note/delete/125437/in+docs Del: other reasons-- https://master.php.net/note/delete/125437 Reject -- https://master.php.net/note/reject/125437 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#215580) next »