Solved

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

Posted on 2011-02-23
7
544 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
  • 4
  • 2
7 Comments
 
LVL 108

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 108

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
The viewer will learn how to dynamically set the form action using jQuery.

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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now