Php 如何打印具有自定义标记的多维数组的所有项?
我有一个类似的多维数组:Php 如何打印具有自定义标记的多维数组的所有项?,php,loops,recursion,multidimensional-array,Php,Loops,Recursion,Multidimensional Array,我有一个类似的多维数组: ( [0] ( title: 'First Group' type: 'group' children: ( [0] ( title: 'Inner Group' type: 'group' children: ( [0] ( title: 'Deep image' type: 'image' )
(
[0] (
title: 'First Group'
type: 'group'
children: (
[0] (
title: 'Inner Group'
type: 'group'
children: (
[0] (
title: 'Deep image'
type: 'image'
)
[1] (
title: 'Another deep image'
type: 'image'
)
)
)
)
)
[1] (
title: 'Root depth image'
type: 'image'
)
)
我希望能够通过这个数组和它的所有子元素(这个数组可以是无限递归的)将内容打印/回显到浏览器
我的问题是能够为不同类型的项指定特定的标记(甚至对每个项执行条件)
仅当每个图像与正则表达式匹配时,才打印出其标题等
我能理解的唯一递归排序数组的方法必须是一个在找到子项时调用自身的方法/函数,但如果是这种情况,我似乎无法执行条件
如果存在循环过程(不使用函数),这可能是最合适的选项,因此在每个子级,我可以对每个项执行任何必需的条件。
我尝试了while和foreach循环,但没有效果
NB:我从两个非递归的源数组编译了这个数组,如果这有助于解决问题,我可以使用这些具有相关键的数组。您只能递归地解决这个问题,因为输入大小未定义 话虽如此,您可以尝试以下方式:
$regexp_map = array(
'group' => 'some regexp',
'image' => 'some other regexp'
);
function walk($array) {
$output = '';
foreach($array as $obj) {
if(is_array($obj['children'])) {
$output .= walk($value['children']); //RECURSION
} else {
$regexp = $regexp_map[$obj['type']];
if(preg_match($regexp, $obj['title']))
$output .= $obj['title'];
}
}
return $output;
}
如果函数过于复杂,则需要一个处理程序与递归函数一起执行该工作
function walk($array, $handler)
{
foreach($array as $obj) {
if(is_array($obj['children'])) {
walk($obj['children'], $handler); //RECURSION
} else {
$handler($obj);
}
}
}
然后为“image”类型的每个数组元素编写处理程序函数、regexp或它应该执行的任何作业
function array_handler ($single_array){
#Do some regexp or more complex job here.
print $single_array['url']."<br/>";
}
基本上,我们定义了一个名为walk的新方法,它的行为类似于foreach和while,但代码块将由处理程序为每个元素执行
完整的代码
<?php
$myarray = array( array (
"title" => 'First Group',
"type" => 'group',
"children" => array (
array ("title" => 'Inner Group',
"type" => "group",
"children" => array (
array (
"title" => "Deep Image",
"type" => 'image',
"url" => "image.jpg"
),
array (
"title" => 'Another Deep Image',
"type" => 'image',
"url" => "image2.jpg"
)
)
)
)
),
array (
"title" => 'Root Deep Image',
"type" => 'image',
"url" => "image.jpg"
)
);
function walk($array, $handler)
{
foreach($array as $obj) {
if(is_array($obj['children'])) {
walk($obj['children'], $handler); //RECURSION
} else {
$handler($obj);
}
}
}
function array_handler ($single_array){
print $single_array['url']."<br/>";
}
walk ($myarray, 'array_handler');
?>
谢谢你的提示。尽管正则表达式只是我希望能够处理数据的一个例子,因此,我可以看到这个函数变得越来越复杂,以允许我需要的许多不同的操作途径,并在代码中使用walk方法作为通用模块,并在任何地方使用它,就像您使用foreach/whilet一样。谢谢,这个概念正是我想要的。实际上,我已经扩展了它,允许字符串带有占位符或自定义函数,具体取决于每个项目所需的内容
<?php
$myarray = array( array (
"title" => 'First Group',
"type" => 'group',
"children" => array (
array ("title" => 'Inner Group',
"type" => "group",
"children" => array (
array (
"title" => "Deep Image",
"type" => 'image',
"url" => "image.jpg"
),
array (
"title" => 'Another Deep Image',
"type" => 'image',
"url" => "image2.jpg"
)
)
)
)
),
array (
"title" => 'Root Deep Image',
"type" => 'image',
"url" => "image.jpg"
)
);
function walk($array, $handler)
{
foreach($array as $obj) {
if(is_array($obj['children'])) {
walk($obj['children'], $handler); //RECURSION
} else {
$handler($obj);
}
}
}
function array_handler ($single_array){
print $single_array['url']."<br/>";
}
walk ($myarray, 'array_handler');
?>