note 115203 added to function.in-array
| From: | Nouniz | Date: | Thu, 12 Jun 2014 08:00:08 +0000 |
| Subject: | note 115203 added to function.in-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-199661@lists.php.net to get a copy of this message | ||
fonction qui permet de vérifier si un élément est dans un tableau ou Ã
l'intérieur d'un tableau qui est dans un autre tableau.
Exemple :
<?php
$array = array ( [0] => Array ( [id_droit] => 1 [nom_droit] => ajouter une annonce
personnelle ) [1] => Array ( [id_droit] => 2 [nom_droit] => modifier une annonce
personnelle ) [2] => Array ( [id_droit] => 3 [nom_droit] => supprimer une annonce
personnelle ) )
$ok = in_multiarray('1', $array); //$ok = true si 1 se trouve dans le tableau
//ou
$ok = in_multiarray("ajouter une annonce personnelle", $array); //$ok = true si
"ajouter une annonce personnelle" se trouve dans le tableau
?>
<?php
/**
* fonction qui permet de vérifier si un élément est dans un tableau ou Ã
l'intérieur d'un sous-tableau.
* @param string $elem : l'élément recherché
* @param array $array : tableau dans lequel on veut savoir si notre élément existe
* @return bolean $ok : true si $elem est dans le tableau $array
*/
function in_multiarray($elem, $array)
{
//print_r($array);
//si $array est un tableau
if(is_array($array))
{
//echo ' - C EST UN TABLEAU ! - <br/>';
//print_r($array);
//echo '<br/>';
//on regarde si $elem est dans $array
if(in_array($elem,$array))
{
//echo ' - C EST OK - <br />';
$ok = true;
}
else
{
//echo ' - NE SE TROUVE PAS DANS LE TABLEAU - <br />';
//sinon : on ne trouve pas $elem dans $array, pour chaque tableau dans $array, on
relance la fonction
foreach($array as $val => $value)
{
if(isset($ok) && $ok){break;}
$ok = in_multiarray($elem, $value);
}
}
}
else
{
//echo ' - CE N EST PAS UN TABLEAU ! - <br/>';
//ce n'est pas un tableau
$ok = false;
}
return $ok;
}
?>
----
Server IP: 72.52.91.14
Probable Submitter: 80.11.100.78
----
Manual Page -- http://php.net/manual/en/function.in-array.php
Edit -- https://master.php.net/note/edit/115203
Del: integrated -- https://master.php.net/note/delete/115203/integrated
Del: useless -- https://master.php.net/note/delete/115203/useless
Del: bad code -- https://master.php.net/note/delete/115203/bad+code
Del: spam -- https://master.php.net/note/delete/115203/spam
Del: non-english -- https://master.php.net/note/delete/115203/non-english
Del: in docs -- https://master.php.net/note/delete/115203/in+docs
Del: other reasons-- https://master.php.net/note/delete/115203
Reject -- https://master.php.net/note/reject/115203
Search -- https://master.php.net/manage/user-notes.php