note 106713 added to function.in-array

From: Date: Wed, 30 Nov 2011 12:51:07 +0000
Subject: note 106713 added to function.in-array
Groups: php.notes 
Request: Send a blank email to php-notes+get-184818@lists.php.net to get a copy of this message
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 "; } ?> ---- Server IP: 202.65.157.132 Probable Submitter: 120.138.102.114 ---- Manual Page -- http://www.php.net/manual/en/function.in-array.php Edit -- https://master.php.net/note/edit/106713 Del: integrated -- https://master.php.net/note/delete/106713/integrated Del: useless -- https://master.php.net/note/delete/106713/useless Del: bad code -- https://master.php.net/note/delete/106713/bad+code Del: spam -- https://master.php.net/note/delete/106713/spam Del: non-english -- https://master.php.net/note/delete/106713/non-english Del: in docs -- https://master.php.net/note/delete/106713/in+docs Del: other reasons-- https://master.php.net/note/delete/106713 Reject -- https://master.php.net/note/reject/106713 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#184818) next »