string(11) "Electronics"-6ren">
gpt4 book ai didi

php - 如何在带有嵌套数组和对象的 php 中使用递归函数?

转载 作者:行者123 更新时间:2023-12-05 02:21:01 25 4
gpt4 key购买 nike

我使用 simplexmlelement 在 foreach 循环中输出了一个这样的数组。

array(16) {
["Binding"]=>
string(11) "Electronics"
["Brand"]=>
string(8) "Micromax"
["Feature"]=>
array(4) {
[0]=>
string(29) "80 centimeters LED 1366 x 768"
[1]=>
string(55) "Connectivity - Input: HDMI*1, USB*1, Component*1, VGA*1"
[2]=>
string(405) "Installation: For requesting installation/wall mounting/demo of this product once delivered, please directly call Micromax support on 1860-500-8899."
[3]=>
string(88) "Warranty Information: 1 year warranty provided by the manufacturer from date of purchase"
}
["ItemDimensions"]=>
object(SimpleXMLElement)#300 (4) {
["Height"]=>
string(4) "1693"
["Length"]=>
string(4) "2898"
["Weight"]=>
string(4) "1213"
["Width"]=>
string(3) "315"
}
["Label"]=>
string(8) "Micromax"
["Manufacturer"]=>
string(8) "Micromax"
["Model"]=>
string(10) "32T7250MHD"
["MPN"]=>
string(14) "MCX_32T7250MHD"
["PackageDimensions"]=>
object(SimpleXMLElement)#301 (4) {
["Height"]=>
string(3) "680"
["Length"]=>
string(4) "3250"
["Weight"]=>
string(4) "2015"
["Width"]=>
string(4) "2260"
}
["PackageQuantity"]=>
string(1) "1"
["PartNumber"]=>
string(14) "MCX_32T7250MHD"
["ProductGroup"]=>
string(2) "CE"
["ProductTypeName"]=>
string(10) "TELEVISION"
["Publisher"]=>
string(8) "Micromax"
["Studio"]=>
string(8) "Micromax"
["Title"]=>
string(52) "Micromax 32T7250MHD 80cm (32 inches) HD Ready LED TV"
}

我正在使用递归函数将几乎所有内容从这个数组输出到 html。我的递归函数如下所示:

function recurseTree($var){
$out = '<li>';
foreach($var as $k=>$v){
if( is_array($v) || is_object($v) ){
$out .= '<ul>'.recurseTree($v).'</ul>';
}else{
$out .= '<li>' .$k .': ' .$v .'</li>';
}
}
return $out.'</li>';
}

我得到的输出是这样的:

<ul>
<li></li>
<li>Binding: Electronics</li>
<li>Brand: Micromax</li>
<ul>
<li></li>
<li>0: 80 centimeters LED 1366 x 768</li>
<li>1: Connectivity - Input: HDMI*1, USB*1, Component*1, VGA*1</li>
<li>2: Installation: For requesting installation/wall mounting/demo of this product once delivered, please directly call Micromax support on 1860-500-8899.</li>
<li>3: Warranty Information: 1 year warranty provided by the manufacturer from date of purchase</li>
</ul>
<ul>
<li>
<ul>
<li></li>
</ul>
<ul>
<li></li>
</ul>
<ul>
<li></li>
</ul>
<ul>
<li></li>
</ul>
</li>
</ul>
<li>Label: Micromax</li>
<li>Manufacturer: Micromax</li>
<li>Model: 32T7250MHD</li>
<li>MPN: MCX_32T7250MHD</li>
<ul>
<li>
<ul>
<li></li>
</ul>
<ul>
<li></li>
</ul>
<ul>
<li></li>
</ul>
<ul>
<li></li>
</ul>
</li>
</ul>
<li>PackageQuantity: 1</li>
<li>PartNumber: MCX_32T7250MHD</li>
<li>ProductGroup: CE</li>
<li>ProductTypeName: TELEVISION</li>
<li>Publisher: Micromax</li>
<li>Studio: Micromax</li>
<li>Title: Micromax 32T7250MHD 80cm (32 inches) HD Ready LED TV</li>
</ul>

如您所见,对象中数组的键和值完全缺失。 (例如,名称为 ["ItemDimensions"] 的对象未出现在输出中。

["ItemDimensions"]=>
object(SimpleXMLElement)#300 (4) {
["Height"]=>
string(4) "1693"
["Length"]=>
string(4) "2898"
["Weight"]=>
string(4) "1213"
["Width"]=>
string(3) "315"
}

如何使用递归函数输出这些对象?另请注意,数组的键名称也丢失了(例如:名称 ["Feature"] 未出现在输出中)。

如何输出这些丢失的数据?谢谢。

最佳答案

数组的键没有出现,因为您在查找数组/对象时根本没有在 if 条件中使用它们。我会将函数修改为如下所示:

function recurseTree($var){
$output = '';
foreach($var as $k=>$v){
if( is_array($v) || is_object($v) ){
$output .= '<li>' . $k . '<ul>'.recurseTree($v).'</ul></li>';
}else{
$output .= '<li>' .$k .': ' .$v .'</li>';
}
}
return $output;
}

现在至于为什么没有返回您的对象键/值,我不太确定。我已经使用 SimpleXmlElement 对象以及 stdClass 对其进行了测试,它们都工作得很好

关于php - 如何在带有嵌套数组和对象的 php 中使用递归函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36658158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com