在PHP数组中显示值

时间:2017-07-23 09:46:18

标签: php multidimensional-array

我在显示PHP数组中的数据时遇到问题。我的数组看起来像这样:

    Array
    (
        [July] => Array
            (
                [0] => Array
                    (
                        [ETitle] => Launch Party
                        [EStart] => 2017-07-26T09:00:00
                        [EEnd] => 2017-04-28T17:00:00
                        [ELink] => http://example.dev/events/launch-party
                        [EStartMonth] => July
                        [EStartYear] => 2017
                    )

            )

        [August] => Array
            (
                [1] => Array
                    (
                        [ETitle] => Open Day
                        [EStart] => 2017-08-10T00:00:00
                        [EEnd] => 2017-08-11T00:00:00
                        [ELink] => http://example.dev/events/open-day
                        [EStartMonth] => August
                        [EStartYear] => 2017
                    )

                [2] => Array
                    (
                        [ETitle] => Dama Google Event
                        [EStart] => 2017-08-20T02:00:00-07:00
                        [EEnd] => 2017-08-20T03:00:00-07:00
                        [EStartMonth] => August
                        [EStartYear] => 2017
                        [ELink] => https://www.google.com/calendar/event?eid=NjdsZW9ydmE4bWlmOHRnZ3J0dGw3MTVlamIgcnY3YTZyMTlmMjQyMHZvcmFkcWNrbW1zdG9AZw
                    )

            )

    )

我需要在页面上显示在这样的标题下分组的项目列表,格式为html:

    <h2>July</h2>
    <ul>
    <li><a href="http://example.dev/events/launch-party">Launch Party</a></li>
    </ul>

    <h2>Augast</h2>
    <ul>
    <li><a href="http://example.dev/events/open-day">Open Day</a></li>
    <!-- if link contains "google" string, link needs target attribute -->
    <li><a target="_blank" href="https://www.google.com/calendar/event?eid=NjdsZW9ydmE4bWlmOHRnZ3J0dGw3MTVlamIgcnY3YTZyMTlmMjQyMHZvcmFkcWNrbW1zdG9AZw">Dama Google Event</a></li>
    </ul>

我已经尝试过堆栈溢出的各种解决方案但是当我尝试递归循环时,我一直以无效的索引警告结束。我如何解决这个问题,并按照描述构建HTML列表?

编辑:使用真实HTML更新预期结果

2 个答案:

答案 0 :(得分:1)

查看PHP中的foreach循环。

假设您的数组存储在名为$events的变量中:

foreach ($events as $monthName => $month) {
  echo '<h2>' . $monthName . '</h2>';
  echo '<ul>';
  foreach ($month as $event) {
    if (strpos(parse_url($event['ELink'], PHP_URL_HOST), 'google') !== false) {
      $target = ' target="_blank"';
    } else {
      $target = '';
    }
    echo '<li><a' . $target . ' href="' . $event['ELink'] . '">' . $event['ETitle'] . '</a></li>';
  }
  echo '</ul>';
}

如果网址主机包含&#34; google&#34;我还添加了target="_blank"属性。

尝试here

答案 1 :(得分:1)

你是如何尝试foreach循环的?这是一个简短的例子,说明你的数组在php中应该如何迭代ist。

外部foreach迭代几个月并打印他们的名字。 第二个/内部foreach遍历月中的所有事件(?)。 HTML输出就是一个例子。

<?php
$data = array (
    'July' => array(
            array(

                    'ETitle' => 'Launch Party',
                    'EStart' => '2017-07-26T09:00:00',
                    'EEnd' => '2017-04-28T17:00:00',
                    'ELink' => 'http://example.dev/events/launch-party',
                    'EStartMonth' => 'July',
                    'EStartYear' => 2017
                )

        ),

    'August' => array
        (
            array(

                    'ETitle' => 'Open Day',
                    'EStart' => '2017-08-10T00:00:00',
                    'EEnd' => '2017-08-11T00:00:00',
                    'ELink' => 'http://example.dev/events/open-day',
                    'EStartMonth' => 'August',
                    'EStartYear' => 2017
                ),

            array (

                    'ETitle' => 'Dama Google Event',
                    'EStart' => '2017-08-20T02:00:00-07:00',
                    'EEnd' => '2017-08-20T03:00:00-07:00',
                    'EStartMonth' => 'August',
                    'EStartYear' => 2017,
                    'ELink' => 'https://www.google.com/calendar/event?eid=NjdsZW9ydmE4bWlmOHRnZ3J0dGw3MTVlamIgcnY3YTZyMTlmMjQyMHZvcmFkcWNrbW1zdG9AZw'
                )

        )

);

//echo '<pre>';
//var_dump($data);

$month = null;
foreach($data as $key => $dates) {
    if ($month !== $key) {
        $month = $key;
        echo '<h1>'.$month.'<h1>';
    }

    if (!empty($dates)) {
        foreach($dates as $data) {
            $title = (isset($data['ETitle'])) ? $data['ETitle'] : 'no title ???';
            $link = (isset($data['ELink'])) ? $data['ELink'] : '#';
            echo '<a href="'.$link.'">' . $title . '</a>';
        }
    }
}
?>

您可以在php sandbox中测试此示例。