php 按值搜索关联数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12376992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:26:51  来源:igfitidea点击:

search associative array by value

php

提问by Johan

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] => Modeland get [_content] => NIKON D5100.

我正在从 flickrs API 获取一些 JSON。我的问题是 exif 数据的顺序取决于相机。所以我不能硬编码一个数组编号来获得,例如,下面的相机型号。PHP 是否有任何内置方法来搜索关联数组值并返回匹配的数组?在下面的示例中,我想搜索[label] => Model并获取[_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
                )

        )

采纳答案by GolezTrol

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

据我所知,没有这样的功能。有array_search,但它并不完全符合您的要求。

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

回答by Arief Hidayatulloh

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

在更新的 PHP 版本中,特别是 PHP 5 >= 5.5.0,上面的函数可以工作。

回答by moldypenguins

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

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

array_map() 函数将数组的每个值发送到用户创建的函数,并返回一个包含由用户创建的函数给出的新值的数组。在这种情况下,我们将返回标签。

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

array_search() 函数在数组中搜索值并返回键。(在这种情况下,我们正在从 array_map 返回的数组中搜索标签值“模型”)

回答by Sean Bright

This would be fairly trivial to implement:

这将是相当简单的实现:

$model = '';

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

回答by MrSil

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

回答by ioannis.th

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

以下函数也可以找到 a、b 和 c

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

回答by prabeen giri

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!!

据我所知,PHP 没有内置的多维数组搜索功能。它仅用于索引和关联数组。因此,您必须编写自己的搜索功能!!