Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
551 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 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 Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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.
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…
The viewer will learn how to dynamically set the form action using jQuery.

688 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