search associative array by value

$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.


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');

This would be fairly trivial to implement:

$model = '';

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

$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')

Tags:

Php