Solved

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

Posted on 2011-02-23
7
548 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 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
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!

 
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

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.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 …

713 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