PHP过滤器子数组


PHP filter sub-array

拥有以下带有库的xml文件

http://pastebin.com/GFykW0tc

我需要显示标签(隐藏在字段2中)

所以我创建了以下php代码

<?php $galleries = eg_return_gallery();
    foreach ($galleries['projects']['items'] as $image)
 { 
  echo $image['field-2']
  } 
    ?>

如何过滤重复的标签,从而只显示唯一的标签?

eg_return_gallery-顾名思义,返回php数组(见下面的示例)

array(3) {
  ["name"]=>
  string(7) "galeria"
  ["title"]=>
  string(4) "Test"
  ["items"]=>
  array(1) {
    [0]=>
    array(10) {
      ["filename"]=>
      string(27) "uploads/galeria-foty/01.jpg"
      ["width"]=>
      int(1050)
      ["height"]=>
      int(740)
      ["thumb-0"]=>
      array(3) {
        ["filename"]=>
        string(52) "ExtraGallery/thumbs/galeria-foty/01-fill-200-300.jpg"
        ["width"]=>
        int(200)
        ["height"]=>
        int(300)
      }
      ["thumb-1"]=>
      array(3) {
        ["filename"]=>
        string(0) "ExtraGallery/thumbs/galeria-foty/01-10-0-200-300-200-300.jpg"
        ["width"]=>
        int(100)
        ["height"]=>
        int(150)
      }
      ["field-0"]=>
      string(10) "Test text"
      ["field-1"]=>
      string(4) "text"
      ["field-2"]=>
      bool(true)
      ["field-3"]=>
      string(0) ""
      ["field-4"]=>
      string(0) ""
    }
  }
}

您可以将每个标签存储在一个新数组中,然后从该数组中删除重复的标签:

<?php 
    $galleries = eg_return_gallery();
    $tags = array();
    foreach ($galleries['projects']['items'] as $image) { 
      $tags[] = $image['field-2'];
    } 
    $tags = array_unique($tags);   
    print_r($tags); 
?>

我建议您查看eg_return_gallery的源代码及其工作方式,但我建议您使用其中一个库http://php.net/manual/en/refs.xml.php,特别是SimpleXML。

但我认为您的XML结构有问题,所以我建议您阅读XML规范http://www.w3.org/TR/2000/REC-xml-20001006.