PHP multidimensional array search by value

前端 未结 23 3233
情歌与酒
情歌与酒 2020-11-21 04:45

I have an array where I want to search the uid and get the key of the array.

Examples

Assume we have the following 2-dimensional array:

<
相关标签:
23条回答
  • 2020-11-21 05:29

    you can use this function ; https://github.com/serhatozles/ArrayAdvancedSearch

    <?php 
    include('ArraySearch.php');
    
    $query = "a='Example World' and b>='2'";
    
    $Array = array(
    'a' => array('d' => '2'),
    array('a' => 'Example World','b' => '2'),
    array('c' => '3'), array('d' => '4'),
    );
    
    $Result = ArraySearch($Array,$query,1);
    
    echo '<pre>';
    print_r($Result);
    echo '</pre>'; 
    
    // Output:
    // Array
    // (
    //    [0] => Array
    //        (
    //            [a] => Example World
    //            [b] => 2
    //        )
    //
    // )
    
    0 讨论(0)
  • 2020-11-21 05:29

    Just share, maybe can like this.

    if( ! function_exists('arraySearchMulti')){
    function arraySearchMulti($search,$key,$array,$returnKey=false)
    {
        foreach ($array as $k => $val) {
            if (isset($val[$key])) {
                if ((string)$val[$key] == (string)$search) {
                    return ($returnKey ? $k : $val);
                }
            }else{
                return (is_array($val) ? arraySearchMulti($search,$key,$val,$returnKey) : null);
            }
        }
        return null;
    }}
    
    0 讨论(0)
  • 2020-11-21 05:31

    Even though this is an old question and has an accepted answer, Thought i would suggest one change to the accepted answer.. So first off, i agree the accepted answer is correct here.

    function searchArrayKeyVal($sKey, $id, $array) {
       foreach ($array as $key => $val) {
           if ($val[$sKey] == $id) {
               return $key;
           }
       }
       return false;
    }
    

    Replacing the preset 'uid' with a parameter in the function instead, so now calling the below code means you can use the one function across multiple array types. Small change, but one that makes the slight difference.

        // Array Data Of Users
    $userdb = array (
        array ('uid' => '100','name' => 'Sandra Shush','url' => 'urlof100' ),
        array ('uid' => '5465','name' => 'Stefanie Mcmohn','url' => 'urlof100' ),
        array ('uid' => '40489','name' => 'Michael','url' => 'urlof40489' ),
    );
    
    // Obtain The Key Of The Array
    $arrayKey = searchArrayKeyVal("uid", '100', $userdb);
    if ($arrayKey!==false) {
        echo "Search Result: ", $userdb[$arrayKey]['name'];
    } else {
        echo "Search Result can not be found";
    }
    

    PHP Fiddle Example

    0 讨论(0)
  • 2020-11-21 05:32

    Here is one liner for the same,

    $pic_square = $userdb[array_search($uid,array_column($userdb, 'uid'))]['pic_square'];
    
    0 讨论(0)
  • 2020-11-21 05:33

    I had to use un function which finds every elements in an array. So I modified the function done by Jakub Truneček as follow:

    function search_in_array_r($needle, $array) {
        $found = array();
        foreach ($array as $key => $val) {
            if ($val[1] == $needle) {
                array_push($found, $val[1]);
            }
        }
        if (count($found) != 0)
            return $found;
        else
            return null;
    }
    
    0 讨论(0)
  • 2020-11-21 05:33
    $a = ['x' => ['eee', 'ccc'], 'b' => ['zzz']];
    
    $found = null;
    $search = 'eee';
    
    array_walk($a, function ($k, $v) use ($search, &$found) {
        if (in_array($search, $k)) {
            $found = $v;
        }
    });
    
    var_dump($found);
    
    0 讨论(0)
提交回复
热议问题