PHP 过滤子数组
PHP filter sub-array
具有以下 xml 文件,其中包含画廊
我需要显示标签(隐藏在字段 2 中)
所以我创建了以下 php 代码
<?php $galleries = eg_return_gallery();
foreach ($galleries['projects']['items'] as $image)
{
echo $image['field-2']
}
?>
如何过滤重复的标签,以便只显示唯一的标签?
eg_return_gallery - 顾名思义 return 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。
具有以下 xml 文件,其中包含画廊
我需要显示标签(隐藏在字段 2 中)
所以我创建了以下 php 代码
<?php $galleries = eg_return_gallery();
foreach ($galleries['projects']['items'] as $image)
{
echo $image['field-2']
}
?>
如何过滤重复的标签,以便只显示唯一的标签?
eg_return_gallery - 顾名思义 return 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。