Quantcast
Channel: RaGEZONE - MMO Development Forums
Viewing all articles
Browse latest Browse all 33342

PHP [HELP] PHP Array search function

$
0
0
Hello i got a problem, i have created costum array search function

PHP Code:

function array_find($find,array $array,$return ARRAY_FIND_RETURN_BOOL)
{
    
$result false;

    foreach(
$array as $index => $value)
    {
        if(
is_array($value) == true)
        {
            
$result array_find($find,$value,$return);

            if(
$result == false)
            {
                continue;
            }
        }
        else
        {
            if(
$value == $find)
            {
                if(
$return == ARRAY_FIND_RETURN_BOOL)
                {
                    
$result true;
                }
                elseif(
$return == ARRAY_FIND_RETURN_ARRAY)
                {
                    
$result $array;
                }
                else
                {
                    
$result $index;
                }
                break;
            }
        }
    }

    return 
$result;


the problem is function will ignore first item in array

PHP Code:

$testArray = array(
    
=> array
    (
        
'Name'      => 'App',
        
'Class'     => 'System\Application',
        
'Enabled'   => 1
    
),
    
=> array
    (
        
'Name'      => 'Router',
        
'Class'     => 'System\Components\Router',
        
'Enabled'   => 1
    
),
); 

if i try to find for example 'Router' everything works as expected but if i wan't to find 'App' the function will return false as value do not exist in array

http://easycaptures.com/fs/uploaded/874/4316690019.png
http://easycaptures.com/fs/uploaded/874/5374100107.png

full code
PHP Code:

define('ARRAY_FIND_RETURN_INDEX',     2);
define('ARRAY_FIND_RETURN_ARRAY',     3);
define('ARRAY_FIND_RETURN_BOOL',      4);

function 
array_find($find,array $array,$return ARRAY_FIND_RETURN_BOOL)
{
    
$result false;

    foreach(
$array as $index => $value)
    {
        if(
is_array($value) == true)
        {
            
$result array_find($find,$value,$return);

            if(
$result == false)
            {
                continue;
            }
        }
        else
        {
            if(
$value == $find)
            {
                if(
$return == ARRAY_FIND_RETURN_BOOL)
                {
                    
$result true;
                }
                elseif(
$return == ARRAY_FIND_RETURN_ARRAY)
                {
                    
$result $array;
                }
                else
                {
                    
$result $index;
                }
                break;
            }
        }
    }

    return 
$result;
}


$testArray = array(
    
=> array
    (
        
'Name'      => 'App',
        
'Class'     => 'System\Application',
        
'Enabled'   => 1
    
),
    
=> array
    (
        
'Name'      => 'Router',
        
'Class'     => 'System\Components\Router',
        
'Enabled'   => 1
    
),
);


echo 
'<b>array_find</b><br>';
echo 
'Find value "Router" :: Mode ARRAY_FIND_RETURN_ARRAY<pre>'.var_export(array_find('Router',$testArray,ARRAY_FIND_RETURN_ARRAY),true).'</pre>';
echo 
'Find value "Router" :: Mode ARRAY_FIND_RETURN_BOOL<pre>'.var_export(array_find('Router',$testArray,ARRAY_FIND_RETURN_BOOL),true).'</pre>';
echo 
'Find value "Router" :: Mode ARRAY_FIND_RETURN_INDEX<pre>'.var_export(array_find('Router',$testArray,ARRAY_FIND_RETURN_INDEX),true).'</pre>';
echo 
'<b>Actual Array</b><br>';
echo 
'<pre>'.var_export($testArray,true).'</pre>'

edit:

Anyway i fixed it for got to add loop break

PHP Code:

if($result == false)
{
    continue;
}
else
{
    break;


http://easycaptures.com/fs/uploaded/874/9716920288.png

Viewing all articles
Browse latest Browse all 33342

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>