2012-07-04 29 views
2

如果有一個相當大的以下形式的多維數組(flickr EXIF數據)。CakePHP 2.1:使用Set來搜索值

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

有沒有一種快速有效的方法來提取一定的值數組,即我要尋找的關鍵「標籤」的價值「鏡頭」,並得到一個數組返回值:

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

回答

1

Set::classicExtract($exifarray, '{n}.tag');將提取所有的標籤,你見過。

Set::matchesCakeBook 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。一探究竟。