Solved

PHP recursive multidimentional array search function

Posted on 2011-03-02
4
390 Views
Last Modified: 2012-05-11
at the moment the function is as follows - but my tired brain cannot see too well if this will work as wanted.
function str_in_array($needle, $haystack, $sw = 1) {
	$ret = false;
    foreach ($haystack as $value[0] => $value[1]) {
		if (is_array($value[$sw])) {
			$ret = str_in_array($needle,$value[$sw],$sw);
		} else if (strtolower($value[$sw])==strtolower($needle)) {
            $ret = true;
        }
    }
    return $ret;
}

Open in new window

it has to step through any dimensional array passed into $haystack and return true if the key or value was matched case insensitively to $needle, depending on the $sw flag (0 for key, 1 for value).
thanks in advance!
0
Comment
Question by:intellisource
  • 2
4 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
From the first impression: did you try to do break after the value is found ?
$ret = true; break;
?
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
No, misunderstood. This seems to be working anyway.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
It seems to work correctly.
<?php // RAY_temp_intellisource.php
error_reporting(E_ALL);
echo "<pre>";

// FROM THE POST AT EE (TIDIED UP TO CODING STANDARDS)
function str_in_array($needle, $haystack, $sw=1)
{
    $ret = FALSE;
    foreach ($haystack as $value[0] => $value[1])
    {
        if (is_array($value[$sw]))
        {
            $ret = str_in_array($needle, $value[$sw], $sw);
        }
        elseif (strtolower($value[$sw]) == strtolower($needle))
        {
            $ret = TRUE;
        }
    }
    return $ret;
}


// SOME TESTS
$arr = array
( 'Red'
, 'Green'
, 'Blue'
)
;
var_dump($arr);
if (str_in_array('Red',    $arr)) echo PHP_EOL . "Red";
if (str_in_array('Yellow', $arr)) echo PHP_EOL . "Yellow";


$arr = array
( array( array( array( 'Red' ))
, 'Green')
, 'Blue'
)
;
var_dump($arr);
if (str_in_array('Red',    $arr)) echo PHP_EOL . "Red";
if (str_in_array('Yellow', $arr)) echo PHP_EOL . "Yellow";
if (str_in_array('Green',  $arr)) echo PHP_EOL . "Green";
if (str_in_array('Blue',   $arr)) echo PHP_EOL . "Blue";

if (str_in_array($arr,     $arr)) echo PHP_EOL . "arr"; // THROWS WARNING

Open in new window

0
 

Author Closing Comment

by:intellisource
Comment Utility
ok thanks... it just seems as if something is preventing an array from being populated.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now