Solved

PHP recursive multidimentional array search function

Posted on 2011-03-02
4
402 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35018328
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
ID: 35018396
No, misunderstood. This seems to be working anyway.
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 35018947
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
ID: 35020095
ok thanks... it just seems as if something is preventing an array from being populated.
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

729 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