note 106713 deleted from function.in-array by cmb
| From: | cmb@php.net | Date: | Sun, 15 Sep 2019 13:09:15 +0000 |
| Subject: | note 106713 deleted from function.in-array by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212194@lists.php.net to get a copy of this message | ||
Note Submitter: rajeevroy15 at gmail dot com dot com
----
is_array function checks only array only and giving incorrect result with multi-dimensional arrays.
Here is a custom function which will give the solution to check Array or Object and Checking of
multi-dimensional arrays and objects as well.
<?php
function in_object($val, $obj){
if($val == ""){
trigger_error("in_object expects parameter 1 must not empty", E_USER_WARNING);
return false;
}
if(!is_object($obj)){
$obj = (object)$obj;
}
foreach($obj as $key => $value){
if(!is_object($value) && !is_array($value)){
if($value == $val){
return true;
}
}else{
return in_object($val, $value);
}
}
return false;
}
?>
Usage :
<?php
$array = array("a", "b", "c"=>array("x",
"y"=>array("p", "q"=>"r")));
if(in_object("r", $arrX)){
echo "r is there ";
}else{
echo "Its not there ";
}
?>