search associative array by value

后端 未结 7 2078
感动是毒
感动是毒 2020-12-09 15:40

I\'m fetching some JSON from flickrs API. My problem is that the exif data is in different order depending on the camera. So I can\'t hard-code an array number to get, for i

7条回答
  •  庸人自扰
    2020-12-09 16:22

    The following function, searches in an associative array both for string values and values inside other arrays. For example , given the following array

     $array= [  "one" => ["a","b"],
                "two" => "c" ];
    

    the following function can find both a,b and c as well

     function search_assoc($value, $array){
             $result = false;
             foreach ( $array as $el){
                 if (!is_array($el)){
                     $result = $result||($el==$value);
                 }
                 else if (in_array($value,$el))
                     $result= $result||true;
                 else $result= $result||false;
             }
             return $result;
         }
    

提交回复
热议问题