Solved

PHP Searching an Array of Arrays for a text value and return the subarray and element pointer

Posted on 2011-02-23
7
550 Views
Last Modified: 2012-06-21
I have an array of arrays exactly as shown below.  I need a FUNCTION that will take a string as a search value to search the "text" keys in the subarrays and RETURN the string value of its associated "id" key.  It also needs to be case insensitive.  If the string to find IS NOT FOUND the function should return a real FALSE not boolean zero because "id" can have a "0" value sometimes.

For example:

$string_to_find = "WHITE";

$found_id_string = myfunction($string_to_find);

if ($found_id_string === FALSE) {
    echo "Sorry, your search text $string_to_find was not found.";
} else {
    echo "Your search for $string_to_find returned an ID of $found_id_string.";
}


Array
(
    [0] => Array
        (
            [id] => 2
            [text] => Red
        )

    [1] => Array
        (
            [id] => 63
            [text] => White
        )

    [2] => Array
        (
            [id] => 3
            [text] => Blue
        )
)
0
Comment
Question by:wfninpa
[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
  • 4
  • 2
7 Comments
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 34961121
You can test this code snippet here:
http://www.laprbass.com/RAY_temp_wfninpa.php
<?php // RAY_temp_wfninpa.php
error_reporting(E_ALL);
echo "<pre>" . PHP_EOL; // MAKE IT EASY TO READ THE OUTPUT

// TEST DATA
$array
= array
( array( 'id'   => 2
       , 'text' => 'Red'
       )
, array( 'id'   => 63
       , 'text' => 'White'
       )
, array( 'id'   => 3
       , 'text' => 'Blue'
       )
,
)
;

// VISUALIZE THE TEST DATA
var_dump($array);

// THE FUNCTION
function lookup($array, $color)
{
    $color = strtoupper($color);
    foreach ($array as $sub_array)
    {
        if ($color == strtoupper($sub_array['text'])) return $sub_array['id'];
    }
    return FALSE;
}

// TEST THE FUNCTION
echo lookup($array, 'WhITe');

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 34961132
BTW, the way to test for FALSE is with the === and not with == because PHP loose-typing will treat empty, NULL zero and FALSE the same if you test with equality instead of identicality.
0
 
LVL 1

Author Comment

by:wfninpa
ID: 34961545
Thanks for the tip, my snippet uses === already, learned that one a long time ago with some of PHP's built in functions.
I am testing your solution shortly.  It looks good.
I will award after testing.
Thanks.
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 1

Author Comment

by:wfninpa
ID: 34962904
Okay,  I tried your snippet and it works great.

I implemented your function and fed it my array but I get this error:

Warning:  Invalid argument supplied for foreach()


A var_dump($myarray) of myarray is below:

array(5) {
  [0]=>
  array(2) {
    ["id"]=>
    string(1) "2"
    ["text"]=>
    string(5) "Black"
  }
  [1]=>
  array(2) {
    ["id"]=>
    string(1) "9"
    ["text"]=>
    string(5) "White"
  }
  [2]=>
  array(2) {
    ["id"]=>
    string(1) "5"
    ["text"]=>
    string(3) "Red"
  }
  [3]=>
  array(2) {
    ["id"]=>
    string(2) "52"
    ["text"]=>
    string(6) "Purple"
  }
  [4]=>
  array(2) {
    ["id"]=>
    string(2) "12"
    ["text"]=>
    string(9) "Turquoise"
  }
}
0
 
LVL 1

Author Comment

by:wfninpa
ID: 34963098
Nevermind.  I found my problem.  $myarray was not available as I was calling your function from inside another function.  I just passed it into the calling function as an argument and now everything works perfectly.

Thank you very much for your help.
0
 
LVL 1

Author Comment

by:wfninpa
ID: 34963137
Sorry, accepted the wrong comment.  Not enough coffee today.

I wish to accept ID:34961121 as the solution, Grade A, 500 Points, Yes to all.  Thanks to Ray_Paseur.
0

Featured Post

Independent Software Vendors: 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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

628 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