PHP multidimensional array search by value

前端 未结 23 3214
情歌与酒
情歌与酒 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:34

    Looks array_filter will be suitable solution for this...

    $userdb=Array
    (
        (0) => Array
            (
                (uid) => '100',
                (name) => 'Sandra Shush',
                (url) => 'urlof100'
            ),
    
        (1) => Array
            (
                (uid) => '5465',
                (name) => 'Stefanie Mcmohn',
                (pic_square) => 'urlof100'
            ),
    
        (2) => Array
            (
                (uid) => '40489',
                (name) => 'Michael',
                (pic_square) => 'urlof40489'
            )
    );
    

    PHP Code

    <?php 
    $search = 5465;
    $found = array_filter($userdb,function($v,$k) use ($search){
      return $v['uid'] == $search;
    },ARRAY_FILTER_USE_BOTH) // With latest PHP third parameter is mandatory.. Available Values:- ARRAY_FILTER_USE_BOTH OR ARRAY_FILTER_USE_KEY  
    
    $values= print_r(array_values($found));
    $keys =  print_r(array_keys($found)); 
    
    0 讨论(0)
  • 2020-11-21 05:34

    You can use array_column for that.

    $user = array_search('5465', array_column($userdb, 'uid'));
    print_r($userdb[$user]);
    

    5465 is the user ID, uid is the key that contains user ID and $userdb is the array which is defined in the question.

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

    In later versions of PHP (>= 5.5.0) you can use this one-liner:

    $key = array_search('100', array_column($userdb, 'uid'));
    
    0 讨论(0)
  • 2020-11-21 05:35

    If question i.e.

    $a = [
         [
           "_id" => "5a96933414d48831a41901f2",
           "discount_amount" => 3.29,
           "discount_id" => "5a92656a14d488570c2c44a2",
         ],
         [
           "_id" => "5a9790fd14d48879cf16a9e8",
           "discount_amount" => 4.53,
           "discount_id" => "5a9265b914d488548513b122",
         ],
         [
           "_id" => "5a98083614d488191304b6c3",
           "discount_amount" => 15.24,
           "discount_id" => "5a92806a14d48858ff5c2ec3",
         ],
         [
           "_id" => "5a982a4914d48824721eafe3",
           "discount_amount" => 45.74,
           "discount_id" => "5a928ce414d488609e73b443",
         ],
        [
           "_id" => "5a982a4914d48824721eafe55",
           "discount_amount" => 10.26,
           "discount_id" => "5a928ce414d488609e73b443",
         ],
       ];
    

    Ans:

    function searchForId($id, $array) {
        $did=0;
        $dia=0;
       foreach ($array as $key => $val) {
           if ($val['discount_id'] === $id) {
               $dia +=$val['discount_amount'];
               $did++;
           }
       }
        if($dia != '') {
            echo $dia;
            var_dump($did);
        }
       return null;
    };
    print_r(searchForId('5a928ce414d488609e73b443',$a));
    
    0 讨论(0)
  • 2020-11-21 05:37

    I modified one of examples below description function array_search. Function searchItemsByKey return all value(s) by $key from multidimensional array ( N levels). Perhaps , it would be useful for somebody. Example:

     $arr = array(
         'XXX'=>array(
                   'YYY'=> array(
                        'AAA'=> array(
                              'keyN' =>'value1'
                       )
                   ),
                  'ZZZ'=> array(
                        'BBB'=> array(
                              'keyN' => 'value2'
                       )
                   )
                  //.....
               )
    );
    
    
    $result = searchItemsByKey($arr,'keyN');
    
    print '<pre>';
    print_r($result);
    print '<pre>';
    // OUTPUT
    Array
    (
      [0] => value1
      [1] => value2
    )
    

    Function code:

    function searchItemsByKey($array, $key)
    {
       $results = array();
    
      if (is_array($array))
      {
        if (isset($array[$key]) && key($array)==$key)
            $results[] = $array[$key];
    
        foreach ($array as $sub_array)
            $results = array_merge($results, searchItemsByKey($sub_array, $key));
      }
    
     return  $results;
    }
    
    0 讨论(0)
  • 2020-11-21 05:37

    my solution:

    function searchArrayForField($array, $field, $value) {
        $i = 0;
        foreach ($array as &$row) {
            if ($row[$field] === $value) {
                return $i;
            }
            $i++
        }
        return '';
    }
    
    0 讨论(0)
提交回复
热议问题