PHP数组到表

PHP数组到表

问题描述:

I am using foreach to get the values of the array in a table but am having difficulty getting the values to show in one line. Please Help.

I've currently got this piece of code but using it only shows the values of [value] underneath eachother:

<?php
foreach($row as $level => $priv){
foreach($priv as $command => $list){
foreach($list as $trigger => $value){       
echo  $value;}}}?>

The Array

[rows] => SimpleXMLElement Object
            (
                [row] => Array
                    (
                        [0] => SimpleXMLElement Object
                            (
                                [rownum] => 1
                                [values] => SimpleXMLElement Object
                                    (
                                        [value] => Array
                                            (
                                                [0] => 12345567
                                                [1] => BC INC
                                                [2] => 9756208
                                                [3] => OTC
                                                [4] => Marketers
                                                [5] => OTC Pink
                                                [6] => BDCG
                                                [7] => 2390
                                                [8] => Misc Products
                                            )

                                    )

                            )

                        [1] => SimpleXMLElement Object
                            (
                                [rownum] => 2
                                [values] => SimpleXMLElement Object
                                    (
                                        [value] => Array
                                            (
                                                [0] => 12345678
                                                [1] => MEDIA, INC.
                                                [2] => 123456
                                                [3] => OTC
                                                [4] => OT
                                                [5] => OTC
                                                [6] => BDDE
                                                [7] => 876856
                                                [8] => Biz Services
                                            )

                                    )

                            )

Assuming $row is your given SimpleXMLElement the code would work like this:

foreach($row->rows->row as $entry) {
    printr $entry->values->value;
}

As you see the print gives out the array where the values are. If you want to have it on some kind of one line you could then go around it with another foreach:

foreach($row->rows->row as $entry) {
    foreach ($entry->values->value as $value) {
       echo $value." "; // echo the current value
    }
    echo "<br>"; // add a br for the output of the next entry
}