【问题标题】:CakePHP 2.1: Using Set to search for a valueCakePHP 2.1:使用 Set 搜索值
【发布时间】:2012-07-05 19:16:30
【问题描述】:

如果有以下形式的相当大的多维数组(flickr EXIF 数据)。

array(
(int) 81 => array(
    'tagspace' => 'Nikon',
    'tagspaceid' => (int) 0,
    'tag' => 'ISOExpansion2',
    'label' => 'ISOExpansion2',
    'raw' => 'Off'
),
(int) 82 => array(
    'tagspace' => 'Nikon',
    'tagspaceid' => (int) 0,
    'tag' => 'LensType',
    'label' => 'Lens Type',
    'raw' => 'G'
),
(int) 83 => array(
    'tagspace' => 'Nikon',
    'tagspaceid' => (int) 0,
    'tag' => 'Lens',
    'label' => 'Lens',
    'raw' => '11-16mm f/2.8'
),...
)

是否有一种快速有效的方法来提取具有某些值的数组,即我会寻找键 'Tag' 的值 'Lens' 并获取一个数组作为返回值:

array(
    'tagspace' => 'Nikon',
    'tagspaceid' => (int) 0,
    'tag' => 'Lens',
    'label' => 'Lens',
    'raw' => '11-16mm f/2.8'
)

另外,这可以使用 Set 完成吗?我仅通过使用$extract = Set::classicExtract($exifarray, '{n}.tag') 实现了以下目标:

array(
      (int) 81 => 'ISOExpansion2',
      (int) 82 => 'LensType',
      (int) 83 => 'Lens',...
)

【问题讨论】:

    标签: cakephp search set cakephp-2.1


    【解决方案1】:

    Set::classicExtract($exifarray, '{n}.tag'); 将提取您所看到的所有标签。

    Set::matches (CakeBook link) 用于查看单个项目或给定的 xpath 是否匹配某些条件。 但是我不确定这是否适用于字符串,但应该可以。 所以如果你已经完成了:

    $data = Set::classicExtract($exifarray, '{n}.tag');
    

    你可以试试:

    foreach($data as $key => $test){
        if(Set:matches(array('0=Lens'), $test[$key])){
        //Logic to run when you have a match.
        }
    }
    

    如果这不起作用,您将不得不以相同的“foreach”方式对其进行正则表达式匹配,顺便说一句,这可能是正确的做法。

    还有一点注意: Set 现在已弃用(自 Cake 2.2 起)。有一个新的更好的数组操作类 - Hash。看看吧。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多