Solved

PHP recursive multidimentional array search function

Posted on 2011-03-02
4
397 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

730 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