【问题标题】:Formatting URL from associative array从关联数组格式化 URL
【发布时间】:2017-02-16 21:36:08
【问题描述】:

我正在尝试根据放入数组中的 url、高度、宽度等来格式化 iframe。这是我到目前为止的代码。

               <div class="row"
                     <div class="col-md-6">
                        <div class="info-box twitter-bg">
                            <div>
                                <?php
                                if ($cfg_array['grafana'] == 'true') {
                                    include 'dashlets.php';
                                    foreach ($dl as $element) {
                                        $url = $element["url"];
                                        $height = $element["height"];
                                        $width = $element["width"];
                                        echo "<iframe src=\"" . $url . " \"" . "height=\"" . $height . " \"" . "width=\"" . $width . " \"" . "frameborder=\"0\">" . " " . "<\iframe>" . " ";
                                    }
                                }
                                ?>
                            </div>
                        </div>
                    </div>

这是来自 dashlets.php 的数组。

    <?php
$dl = array(
    'dashlet1' => array(
        'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=6',
        'height' => '200',
        'width' => '450'
    ),
    'dashlet2' => array(
        'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=7',
        'height' => '200',
        'width' => '450'
    ),
    'dashlet3' => array(
        'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=11',
        'height' => '200',
        'width' => '450'
    ),
    'dashlet4' => array(
        'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=4',
        'height' => '200',
        'width' => '350'
    ),
    'dashlet5' => array(
        'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=8',
        'height' => '200',
        'width' => '450'
    ),
    'dashlet6' => array(
        'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=10',
        'height' => '200',
        'width' => '450'
    ),
    'dashlet7' => array(
        'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=3',
        'height' => '200',
        'width' => '450'
    ),
    'dashlet8' => array(
        'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=5',
        'height' => '200',
        'width' => '350'
    )
);

我的问题是,循环只在循环内部时从数组中提取第一个值,如果它位于循环括号之外,则只提取最后一个值,如下所示。 我正在尝试从数组项中格式化 多个 url,并将它们放置在彼此水平的 iframe 中。 是不是我做错了什么?

已编辑以添加完整代码,包括 html div。

按照建议将所有内容移至 dashlets.php。我仍然只从第一个数组项中获得返回结果。

<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
$dl = array(
    'dashlet1' => array(
        'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=6',
        'height' => '200',
        'width' => '450'
    ),
    'dashlet2' => array(
        'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=7',
        'height' => '200',
        'width' => '450'
    ),
    'dashlet3' => array(
        'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=11',
        'height' => '200',
        'width' => '450'
    ),
    'dashlet4' => array(
        'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=4',
        'height' => '200',
        'width' => '350'
    ),
    'dashlet5' => array(
        'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=8',
        'height' => '200',
        'width' => '450'
    ),
    'dashlet6' => array(
        'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=10',
        'height' => '200',
        'width' => '450'
    ),
    'dashlet7' => array(
        'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=3',
        'height' => '200',
        'width' => '450'
    ),
    'dashlet8' => array(
        'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=5',
        'height' => '200',
        'width' => '350'
    )
);
foreach ($dl as $element) {
    $url = $element["url"];
    $height = $element["height"];
    $width = $element["width"];
    echo "<iframe src=\"" . $url . "\"" . " height=\"" . $height . "\" " . "width=\"" . $width . "\"" . " frameborder=\"0\">" . " " . "<\iframe>" . "\n";
}

在我的 main.php 中,可以从我的 index.php 视口查看。

            <div class="row"
                 <div class="col-md-6">
                    <div class="info-box twitter-bg">
                        <div>
                            <?php
                            if ($cfg_array['grafana'] == 'true') {
                                include 'dashlets.php';

                            }
                                ?>
                            </div>
                        </div>
                    </div>

【问题讨论】:

  • 您希望 PHP 代码的结果是什么?

标签: php multidimensional-array foreach


【解决方案1】:

我只是试试你的代码,刚刚添加到dashlets.php中:

  <?php
        $dl = array(
            'dashlet1' => array(
                'url' => 'http://1.1.1.1:3333/stuff/stuff?36',
                'height' => '200',
                'width' => '450'
            ),
            'dashlet2' => array(
                'url' => 'http://1.1.1.1:3333/stuff/stuff?37',
                'height' => '200',
                'width' => '450'
                ......
                ......
                'width' => '450'
            ),
            'dashlet8' =>array(
            'url' => 'http://1.1.1.1:3333/stuff/stuff?35',
            'height' => '200',
            'width' => '350'
            )
            );
    ?>

并将 echo 放入循环中:

  include 'dashlets.php';
    foreach ($dl as $element) {
         $url = $element["url"];
         $height = $element["height"];
         $width = $element["width"];
         echo "<iframe src=\"" . $url . "\"" . " height=\"" . $height . "\" " . "width=\"" . $width . "\"" . " frameborder=\"0\">" . " " . "<\iframe>" . "\n";
     }

得到了这个:

<iframe src="http://1.1.1.1:3333/stuff/stuff?36" height="200" width="450" frameborder="0"> <\iframe>
<iframe src="http://1.1.1.1:3333/stuff/stuff?37" height="200" width="450" frameborder="0"> <\iframe>
<iframe src="http://1.1.1.1:3333/stuff/stuff?311" height="200" width="450" frameborder="0"> <\iframe>
<iframe src="http://1.1.1.1:3333/stuff/stuff?34" height="200" width="350" frameborder="0"> <\iframe>
<iframe src="http://1.1.1.1:3333/stuff/stuff?38" height="200" width="450" frameborder="0"> <\iframe>
<iframe src="http://1.1.1.1:3333/stuff/stuff?310" height="200" width="450" frameborder="0"> <\iframe>
<iframe src="http://1.1.1.1:3333/stuff/stuff?33" height="200" width="450" frameborder="0"> <\iframe>
<iframe src="http://1.1.1.1:3333/stuff/stuff?35" height="200" width="350" frameborder="0"> <\iframe>

【讨论】:

  • 正如我所说,我试过了,它只产生了数组中列出的第一个值......
  • 在你的 Dashlets.php 中,你有 吗?
  • 是的,我有 关闭文件,因为其中只列出了数组。我更新了原始帖子以反映该 div 的所有内容。我还将 echo 语句移到循环内,以确保没有混淆。
  • 我也改了几个\",并在回显的末尾添加了一个\n,但这可能不是问题。
  • 不确定为什么我会得到不同的结果。我刚刚尝试将循环移动到我的 dashlets.php,但我仍然只显示第一个数组项。
猜你喜欢
  • 2010-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多