简体   繁体   中英

How to use recursive function in php with nested arrays and objects?

I have an array like this outputed in a foreach loop using 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"
}

I am using a recursive function to output almost everything from this array to html. My recursive function looks like this:

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>';
}

The output that I get is like this:

<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>

As you see, the keys and values of arrays inside the objects are totally missing. (for example the object with the name ["ItemDimensions"] is not appearing in the output.

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

How do I output these objects as well using the recursive function? Also note that key name of arrays are also missing (for example: the name ["Feature"] does not appear in the output).

How can I output these missing data? thanks.

Keys for arrays are not appearing because you are simply not using them inside your if condition when looking for arrays/objects. I would modify the function to something like this:

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;
}

Now as to why your object key/values are not being returned, that I am not too sure. I've tested this with a SimpleXmlElement object as well as with stdClass and they both work just fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM