note 63705 deleted from function.in-array by bjori
| From: | bjori@php.net | Date: | Tue, 28 Mar 2006 20:45:14 +0000 |
| Subject: | note 63705 deleted from function.in-array by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-106881@lists.php.net to get a copy of this message | ||
Note Submitter: tikras [at] hakeris /dot/ co \dot\ uk
Reason: 2
----
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/