Php 从关联数组格式化URL
我试图从URL、高度、宽度等设置一个数组来格式化iframe。这是我到目前为止的代码Php 从关联数组格式化URL,php,multidimensional-array,foreach,Php,Multidimensional Array,Foreach,我试图从URL、高度、宽度等设置一个数组来格式化iframe。这是我到目前为止的代码 <div class="row" <div class="col-md-6"> <div class="info-box twitter-bg"> <div>
<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'
)
);
我刚刚尝试了您的代码,刚刚添加到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";
}
包括“dashlet.php”;
foreach($dl作为$element){
$url=$element[“url”];
$height=$element[“height”];
$width=$element[“width”];
回显“。\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>
如前所述,我尝试过,结果只产生了数组中列出的第一个值……在您的dashlet.php中,您有吗?是的,我有,因为其中只列出了数组。我更新了原始帖子,以反映该部门的所有内容。我还将echo语句移动到了循环中,以确保没有混淆。我更改了几处\“也是,并在回音末尾添加了一个\n,但这可能不是问题。不确定为什么会得到不同的结果。”。我刚刚尝试将循环移动到我的dashlets.php,但仍然只显示了第一个数组项。您希望php代码的结果是什么?
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>