尝试在html标记中添加时,php数组输出错误

时间:2018-10-24 08:59:00

标签: php arrays

我徘徊了为什么我的代码会产生错误的输出。

$array = require 'http://vilavaleaprahovei.ro/kimea/allMarks.php';
$output = array_keys($array);
$option = '';
foreach ($output as $val) {
    $option .= "<option>$val</option>";
}
print_r($option);

预期结果是:

<option>ALCAR STAHLRAD</option>
<option>ENZO</option>
<option>DEZENT</option>
...........

提前谢谢!

4 个答案:

答案 0 :(得分:2)

确保您的allow_url_include = On中有php.ini

如果allMarks.php是一个php文件,您在其中声明并返回一个数组,则应首先打开php脚本标签<?php,然后按如下所示使用return关键字:

<?php
return array (
  'ALCAR STAHLRAD' => 
  array ( ...

答案 1 :(得分:1)

尝试一下,希望它能起作用:

$data = array('ALCAR STAHLRAD' => array('diametru' => array(0 => 15, 6 => 16, 9 => 14, 14 => 13, 20 => 17, 468 => 20,), 'latime' => array(0 => 6, 9 => 5, 10 => 4, 27 => 7, 501 => 3,), 'pcd' => array(0 => 15, 6 => 16, 9 => 14, 14 => 13, 20 => 17, 468 => 20,), 'pcd1' => array(0 => 15, 6 => 16, 9 => 14, 14 => 13, 20 => 17, 468 => 20,), 'et' => array(0 => 15, 6 => 16, 9 => 14, 14 => 13, 20 => 17, 468 => 20,), 'cb' => array(0 => 15, 6 => 16, 9 => 14, 14 => 13, 20 => 17, 468 => 20,),), 'ENZO' => array('diametru' => array(0 => 16, 2 => 17, 3 => 15, 10 => 14, 14 => 18,), 'latime' => array(0 => 6, 1 => 7, 10 => 5, 35 => 8,), 'pcd' => array(0 => 16, 2 => 17, 3 => 15, 10 => 14, 14 => 18,), 'pcd1' => array(0 => 16, 2 => 17, 3 => 15, 10 => 14, 14 => 18,), 'et' => array(0 => 16, 2 => 17, 3 => 15, 10 => 14, 14 => 18,), 'cb' => array(0 => 16, 2 => 17, 3 => 15, 10 => 14, 14 => 18,),), 'DEZENT' => array('diametru' => array(0 => 17, 1 => 16, 6 => 15, 24 => 18, 46 => 14, 80 => 19, 159 => 20,), 'latime' => array(0 => 7, 6 => 6, 20 => 8, 63 => 5, 120 => 4, 159 => 9,), 'pcd' => array(0 => 17, 1 => 16, 6 => 15, 24 => 18, 46 => 14, 80 => 19, 159 => 20,), 'pcd1' => array(0 => 17, 1 => 16, 6 => 15, 24 => 18, 46 => 14, 80 => 19, 159 => 20,), 'et' => array(0 => 17, 1 => 16, 6 => 15, 24 => 18, 46 => 14, 80 => 19, 159 => 20,), 'cb' => array(0 => 17, 1 => 16, 6 => 15, 24 => 18, 46 => 14, 80 => 19, 159 => 20,),), 'DOTZ' => array('diametru' => array(0 => 17, 1 => 15, 3 => 18, 5 => 16, 6 => 19, 65 => 20,), 'latime' => array(0 => 7, 1 => 6, 2 => 8, 71 => 9,), 'pcd' => array(0 => 17, 1 => 15, 3 => 18, 5 => 16, 6 => 19, 65 => 20,), 'pcd1' => array(0 => 17, 1 => 15, 3 => 18, 5 => 16, 6 => 19, 65 => 20,), 'et' => array(0 => 17, 1 => 15, 3 => 18, 5 => 16, 6 => 19, 65 => 20,), 'cb' => array(0 => 17, 1 => 15, 3 => 18, 5 => 16, 6 => 19, 65 => 20,),), 'AEZ' => array('diametru' => array(0 => 16, 1 => 17, 5 => 18, 10 => 19, 28 => 20, 106 => 21, 157 => 22,), 'latime' => array(0 => 7, 1 => 8, 16 => 9, 105 => 6, 106 => 11, 114 => 10,), 'pcd' => array(0 => 16, 1 => 17, 5 => 18, 10 => 19, 28 => 20, 106 => 21, 157 => 22,), 'pcd1' => array(0 => 16, 1 => 17, 5 => 18, 10 => 19, 28 => 20, 106 => 21, 157 => 22,), 'et' => array(0 => 16, 1 => 17, 5 => 18, 10 => 19, 28 => 20, 106 => 21, 157 => 22,), 'cb' => array(0 => 16, 1 => 17, 5 => 18, 10 => 19, 28 => 20, 106 => 21, 157 => 22,),),);
// Above array is your data
$option = '';
foreach ($data as $key => $val) {
    $option .= "<option>$key</option>";
}
print_r(htmlentities($option));

答案 2 :(得分:0)

现在在服务器上是:

DOMSource domSource = new DOMSource(engine.getDocument());
StreamResult result = new StreamResult(outputFile);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.METHOD, "html");
transformer.transform(domSource, result);

但仍不显示选项值

答案 3 :(得分:-1)

您面临的问题是您希望打印html而不在html上呈现它,因此您应该使用

 foreach ($output as $val) {
    $option .= "&lt;option&gt;$val&lt;/option&gt; </br>";
}

代替

foreach ($output as $val) {
    $option .= "<option>$val</option>";
}