简体   繁体   中英

Search in array by block and print result

I did a search here in stackoverflo and mr. Google, but didn't find what I looking for, they all just the result, but the question is from what block and what other information does it has?...
so here is an example of the array I'm working with

    Array
    (
[28] => Array
        (
            [info] => stdClass Object
                (
                    [nombre] => Jonny
                    [midname] => Smush
                    [lastname] => Hugs
                    [age] => 51
                    [street] => South 23
                    [block] => At2
                    [zp] => 01000
                    [city] => Baltimore
                    [state] => MD
                    [country] => US
                    [t_h] => 2343242343
                    [t_m] => 
                    [t_a] => 
                    [email] => dfsgfdgd@sdsd.com
                    [health] => Good
                )

            [emer] => stdClass Object
                (
                    [t_h] => 2343243243
                    [t_w] => 
                    [t_m] => 
                )

            [date] => 2014-04-09 10:46:37
            [news] => no
        )

[29] => Array
        (
            [info] => stdClass Object
                (
                    [nombre] => Tachio
                    [midname] => Magis
                    [lastname] => Dulsh
                    [age] => 23
                    [street] => South 23
                    [block] => At2
                    [zp] => 01000
                    [city] => Baltimore
                    [state] => MD
                    [country] => US
                    [t_h] => 2343242343
                    [t_m] => 
                    [t_a] => 
                    [email] => dfsgfdgd@sdsd.com
                    [health] => Ok
                )

            [emer] => stdClass Object
                (
                    [t_h] => 2343243243
                    [t_w] => 
                    [t_m] => 
                )

            [date] => 2014-04-09 10:46:37
            [news] => no
        ).. way to long to display all

So now that we have the basic idea how the array is, now I need to search for: name, midname, lastname, and age, if this four values are a match within the ID(I forgot to mention that the first key [28]. [29] is the ID) then display the rest of the information from that ID([28], [29]...)
I have to mention that that array is not show anywhere in my app, only the result, so my url is something like: /search.php?d1=Tachio&mn=Magis&ln=Dulsh&a=23
if it find that match in the array then display the rest of the information.

Did I mention that I need the ID from which block the information where found?...

Thank you for taking the time.

There is probably a better way of doing it but you could loop through and find it.

function findUser($userData)
{
    foreach($userData as $key=>$value)
    {
        if($value['info']->nombre == $_GET['d1']
              && $value['info']->midname == $_GET['mn']
              && $value['info']->lastname == $_GET['ln']
              && $value['info']->age == $_GET['a'])
              return array('id'=>$key, 'information'=>$value);
    }
    return false;
}

Now feed the array to the function. If it finds a match it returns an array where the key id contains the numeric id and information contains the information for that user.

Probably worth noting, this is case sensitive.

returns false if not found.

Have fun.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM