Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-02-23
7
Medium Priority
?
553 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 111

Accepted Solution

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

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

916 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