格式化关联数组

时间:2017-02-16 21:36:09

标签: php multidimensional-array foreach

我正在尝试从数组中放置的网址,高度,宽度等格式化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'
    )
);

我的问题是循环只是在循环内部时从数组中拉出第一个值,如果它放在循环括号之外,则只有最后一个值,如下所示。 我正在尝试从数组项中格式化多个网址,并将它们放置在相对于彼此的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>

1 个答案:

答案 0 :(得分: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'
            )
            );
    ?>

将回声置于循环中:

  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>