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

时间:2016-04-15 22:59:51

标签: php arrays recursion simplexml

我使用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"]未出现在输出中)。

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

1 个答案:

答案 0 :(得分:2)

数组的键没有出现,因为在查找数组/对象时,您根本不在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对它进行了测试,它们都运行得很好