?
Solved

PHP recursive multidimentional array search function

Posted on 2011-03-02
4
Medium Priority
?
405 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 111

Accepted Solution

by:
Ray Paseur earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.
Suggested Courses

743 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