按值搜索关联数组

按值搜索关联数组

本文介绍了按值搜索关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从flickrs API中获取一些JSON.我的问题是,exif数据的顺序取决于相机.因此,我无法对一个数组编号进行硬编码以获取例如下面的相机型号. PHP是否有任何内置方法可搜索关联数组值并返回匹配的数组?在下面的示例中,我想搜索[label] => Model并获取[_content] => NIKON D5100.

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

结果:

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
                )

        )

推荐答案

据我所知,没有此类功能.有 array_search ,但是它并不能完全满足您的要求.

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

这篇关于按值搜索关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 00:24