Johan
Johan

Reputation: 35194

search associative array by value

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 instance, the camera model below. Does PHP have any built in methods to search through associative array values and return the matching arrays? In my example below I would like to search for the [label] => Model and get [_content] => NIKON D5100.

Please let me know if you want me to elaborate.

print_r($exif['photo']['exif']);

Result:

Array
(
    [0] => Array
        (
            [tagspace] => IFD0
            [tagspaceid] => 0
            [tag] => Make
            [label] => Make
            [raw] => Array
                (
                    [_content] => NIKON CORPORATION
                )

        )

    [1] => Array
        (
            [tagspace] => IFD0
            [tagspaceid] => 0
            [tag] => Model
            [label] => Model
            [raw] => Array
                (
                    [_content] => NIKON D5100
                )

        )

    [2] => Array
        (
            [tagspace] => IFD0
            [tagspaceid] => 0
            [tag] => XResolution
            [label] => X-Resolution
            [raw] => Array
                (
                    [_content] => 240
                )

            [clean] => Array
                (
                    [_content] => 240 dpi
                )

        )

Upvotes: 40

Views: 89732

Answers (8)

mostafa ali
mostafa ali

Reputation: 19

$data = [
["name"=>"mostafa","email"=>"[email protected]"],
["name"=>"ali","email"=>"[email protected]"],
["name"=>"nader","email"=>"[email protected]"]];

chekFromItem($data,"ali");

function chekFromItem($items,$keyToSearch)
{
$check = false;
foreach($items as $item)
{
    foreach($item as $key)
    {
        if($key == $keyToSearch)
        {
            $check = true;
        }
    }

    if($check == true)
    {break;}
}
return $check;}

Upvotes: 1

moldypenguins
moldypenguins

Reputation: 509

$key = array_search('Model', array_map(function($data) {return $data['label'];}, $exif))

The array_map() function sends each value of an array to a user-made function, and returns an array with new values, given by the user-made function. In this case we are returning the label.

The array_search() function search an array for a value and returns the key. (in this case we are searching the returned array from array_map for the label value 'Model')

Upvotes: 17

ioannis.th
ioannis.th

Reputation: 453

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;
     }

Upvotes: 1

Arief Hidayatulloh
Arief Hidayatulloh

Reputation: 925

$key = array_search('model', array_column($data, 'label'));

In more recent versions of PHP, specifically PHP 5 >= 5.5.0, the function above will work.

Upvotes: 92

GolezTrol
GolezTrol

Reputation: 116110

To my knowledge there is no such function. There is array_search, but it doesn't quite do what you want.

I think the easiest way would be to write a loop yourself.

function search_exif($exif, $field)
{
    foreach ($exif as $data)
    {
        if ($data['label'] == $field)
            return $data['raw']['_content'];
    }
}

$camera = search_exif($exif['photo']['exif'], 'model');

Upvotes: 23

MrSil
MrSil

Reputation: 618

foreach($exif['photo']['exif'] as $row) {
    foreach ($row as $k => $v) {
        if ($k == "label" AND $v == "Model")
            $needle[] = $row["raw"];
    }
}
print_r($needle);

Upvotes: 2

prabeen giri
prabeen giri

Reputation: 803

As far as I know , PHP does not have in built-in search function for multidimensional array. It has only for indexed and associative array. Therefore you have to write your own search function!!

Upvotes: 0

Sean Bright
Sean Bright

Reputation: 120644

This would be fairly trivial to implement:

$model = '';

foreach ($exif['photo']['exif'] as $data) {
    if ($data['label'] == 'Model') {
        $model = $data['raw']['_content'];
        break;
    }
}

Upvotes: 5

Related Questions