我在foreach循环中使用simplexmlelement输出了这样一个数组。
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测试了这一点,它们都能很好地工作