Avec ceci:get article, un tableau assoctivate
//Example array
$arr = array(
'db' => array('host' => 'local', 'user' => 'root'));
public function getParam($name, $default = null)
{
if (array_key_exists($name, $this->params)) {
return $this->params[$name];
}
return $default;
}
je peux obtenir toute gamme db, mais si je veux atteindre le « hôte » uniquement avec la méthode getParam() comment dois-je prolonger La méthode getParam est-elle flexible et fonctionne-t-elle de façon indépendante à quel point le tableau est profond?
Vous devez écrire une fonction récursive pour explorer le tableau –
Si vous utilisez PHP 5.4, vous pouvez simplement utiliser ceci: '$ obj-> getParam (" db ") ['host'];' –