note 63705 added to function.in-array
| From: | tikras | Date: | Tue, 28 Mar 2006 20:06:04 +0000 |
| Subject: | note 63705 added to function.in-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-106879@lists.php.net to get a copy of this message | ||
Seems like recrusion is sometimes very bad for anyone...
You've got learn not to use recrusion as it uses up more
memory and cpu time,
depending on what it is doing.
If you have a look at http://php.net/isset (not sure if it's
there yet), I posted a note about how you should use
recrusion with the same function or not.
It takes a bit of time to understand how
not to use recrusion.
So, I had done a four multidimensional
array checking functions.
Their results are:
<pre>bool(true) (Recrusive in_array)
Execution in 8.70227813721E-05...
<hr />bool(true) (Non Recrusive in_array)
Execution in 4.38690185547E-05...
<hr />string(3) "oqo" (Recrusive custom check)
Execution in 4.88758087158E-05...
<hr />string(3) "oqo" (Non Recrusive custom check)
Execution in 4.19616699219E-05...
</pre>
Sems like my last function, array_get_key_by_value2
beats all of them.
Also, it returns the key name of the item.
I have tested this on a multidimensional array,
which is a quiet short one.
I tested them on a big array:
<pre>bool(true)
Execution in 0.000128984451294...
<hr />bool(true)
Execution in 7.70092010498E-05...
<hr />string(3) "oqo"
Execution in 5.00679016113E-05...
<hr />string(3) "oqo"
Execution in 0.000502109527588...
</pre>
</pre>
Note that the third one is E-05, which
changes the results to:
<pre>bool(true)
Execution in 0.000128984451294...
<hr />bool(true)
Execution in 7.70092010498E-05...
<hr />string(3) "oqo"
Execution in 0.000050067901611...
<hr />string(3) "oqo"
Execution in 0.000502109527588...
</pre>
These results look very strange
The first and third functions are recrusive and
use more memory.
But i think that the third of these, using recrusion
would be the best.
And here is a sample script:
<?php
error_reporting(E_ALL);
echo "<pre>";
/*
* This has been done on PHP 5, though might work on PHP4
*/
function microtime_float(){
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
function in_array2($array,$value) {
if ( in_array($value,$array) ) {
return true;
}
foreach($array as $k => $v){
if ( is_array($v) ) {
return in_array2($v,$value);
}
}
return false;
}
function in_array3($array,$value) {
$items=array();
while ( count($array) != 0 ) {
foreach($array as $k => $vv) {
if ( $vv === $value ) {
return true;
}
if ( is_array($vv) ) {
foreach($vv as $kk => $vvv) {
if ( $vvv === $value ) {
return true;
}
else { $array[$kk]=$vvv; }
}
}
unset($array[$k]);
}
}
return false;
}
function array_get_key_by_value($array,$value,&$key) {
foreach($array as $i => $v) {
if ( $v === $value ) {
return $i;
}
elseif ( is_array($v) ) {
$rr=array_get_key_by_value(
$v,$value,$key
);
if ( $rr !== false ) { return $rr; }
}
}
return false;
}
function array_get_key_by_value2($array,$value) {
$items=array();
while ( count($array) != 0 ) {
foreach($array as $k => $vv) {
if ( $vv === $value ) {
return $k;
}
if ( is_array($vv) ) {
foreach($vv as $kk => $vvv) {
if ( $vvv === $value ) { return $kk; }
else { $array[$kk]=$vvv; }
}
}
unset($array[$k]);
}
}
return false;
}
$search="lama";
$key='';
$arr=array(
"xx"=>"l",
array(
"podo",
"xxz"=>array("oqo"=>"lama")
)
);
$time_start = microtime_float();
var_dump(in_array2($arr,$search));
$time=microtime_float() - $time_start;
echo "Execution in {$time}...\n";
echo "<hr />";
$time_start = microtime_float();
var_dump(in_array2($arr,$search));
$time=microtime_float() - $time_start;
echo "Execution in {$time}...\n";
echo "<hr />";
$time_start = microtime_float();
var_dump( array_get_key_by_value($arr,$search,$key) );
$time=microtime_float() - $time_start;
echo "Execution in {$time}...\n";
echo "<hr />";
$time_start = microtime_float();
var_dump( array_get_key_by_value2($arr,$search,$key) );
$time=microtime_float() - $time_start;
echo "Execution in {$time}...\n";
echo "</pre>";
?>
Thank You
http://wykis.com/
----
Server IP: 84.18.202.180
Probable Submitter: 84.9.162.99
----
X-Spam-Status: No, hits=3.3 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER,
HTML_MESSAGE autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.in-array.php
Edit -- http://master.php.net/note/edit/63705
Del: integrated -- http://master.php.net/note/delete/63705/integrated
Del: useless -- http://master.php.net/note/delete/63705/useless
Del: bad code -- http://master.php.net/note/delete/63705/bad+code
Del: spam -- http://master.php.net/note/delete/63705/spam
Del: non-english -- http://master.php.net/note/delete/63705/non-english
Del: in docs -- http://master.php.net/note/delete/63705/in+docs
Del: other reasons-- http://master.php.net/note/delete/63705
Reject -- http://master.php.net/note/reject/63705
Search -- http://master.php.net/manage/user-notes.php