Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP recursive multidimentional array search function

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
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 …
Suggested Courses

604 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