php数组数据到列表项

时间:2016-06-16 12:05:22

标签: php arrays

我有数组数据,显示数据,但我想在列表项中使用它,例如ulli代码,但我不知道我在这里做的确切方法是我想要的示例可能正在使用whileforeach循环

<ul>
    <li>Demo Item 1</li>
    <li>Demo Description 1</li>
    <li>http://demo-site.com/1</li>
</ul>

<ul>
    <li>Demo Item 2</li>
    <li>Demo Description 2</li>
    <li>http://demo-site.com/2</li>
</ul>

<ul>
    <li>Demo Item 3</li>
    <li>Demo Description 3</li>
    <li>http://demo-site.com/3</li>
</ul>

<ul>
    <li>Demo Item 4</li>
    <li>Demo Description 4</li>
    <li>http://demo-site.com/4</li>
</ul>

<ul>
    <li>Demo Item 5</li>
    <li>Demo Description 5</li>
    <li>http://demo-site.com/5</li>
</ul>

这是我的php代码:

<?php
include_once('simple_html_dom.php');
$xml = simplexml_load_file('http://mywebsite-addr/stats.xml');
$parsed_results_array = array();
foreach($xml as $entry) {
    foreach($entry->match as $item) {
        // $parsed_results_array[] = json_decode(json_encode($item), true);
        $items['title'] = (string) $item->opponent->name;
        $items['description'] = (string) $item->result;
        $items['link'] = (string) $item->url;
        $items['time'] = (string) $item->date;
        $parsed_results_array[] = $items;
    }
}

echo '<pre>';
print_r($parsed_results_array);
echo '</pre>';
?>

以下是可以帮助您的数组数据

Array
(
    [0] => Array
        (
            [title] => Demo Item 1
            [description] => Demo Description 1
            [link] => http://demo-site.com/1
            [time] => Wed 15 Jun 2016, 11:30 PM EDT
        )

    [1] => Array
        (
            [title] => Demo Item 2
            [description] => Demo Description 2
            [link] => http://demo-site.com/2
            [time] => Wed 15 Jun 2016, 10:00 PM EDT
        )

    [2] => Array
        (
            [title] => Demo Item 3
            [description] => Demo Description 3
            [link] => http://demo-site.com/4
            [time] => Wed 15 Jun 2016, 12:45 AM EDT
        )

    [3] => Array
        (
            [title] => Demo Item 4
            [description] => Demo Description 4
            [link] => http://demo-site.com/4
            [time] => Tue 14 Jun 2016, 11:45 PM EDT
        )

    [4] => Array
        (
            [title] => Demo Item 5
            [description] => Demo Description 5
            [link] => http://demo-site.com/5
            [time] => Tue 14 Jun 2016, 10:00 PM EDT
        )

)

4 个答案:

答案 0 :(得分:1)

模板替代方案:

<?php
    // Some nice php code here ...
?>

<?php foreach( $parsed_results_array as $result ): ?>
<ul>
    <?php foreach( $result as $data ): ?>
    <li><?php echo $data ?></li>
    <?php endforeach ?>
</ul>
<?php endforeach ?>    

<?php
    // Some other php code there ...
?>

答案 1 :(得分:0)

foreach ( $parsed_results_array as $arrayKey => $values ) {
    echo '<ul>';
    echo '<li>' . $values['title'] . '</li>';
    echo '<li>' . $values['description'] . '</li>';
    echo '<li>' . $values['link'] . '</li>';
    echo '</ul>';
}

答案 2 :(得分:0)

  

尝试此操作而不创建额外的数组:

include_once('simple_html_dom.php');
$xml = simplexml_load_file('http://mywebsite-addr/stats.xml');
$parsed_results_array = array();

foreach($xml as $entry) {
    foreach($entry->match as $item) {
        echo '<ul>';
        echo '<li>' . (string) $item->opponent->name . '</li>';
        echo '<li>' . (string) $item->result . '</li>';
        echo '<li>' . (string) $item->url . '</li>';
        echo '</ul>';
    }
}

答案 3 :(得分:0)

你真的不需要任何一个数组,但至少放弃这一行

$parsed_results_array[] = $items;

并将其替换为

echo "
<ul>
<li>$items[title]</li>
<li>$items[description]</li>
<li>$items[link]</li>
</ul>
";