我正在看下面的样本
$array = [
'Name1' => [
'diametru' => [7,6]
],
'othername' => []
];
$output = array_keys($array);
$option = '';
foreach ($output as $val) {
$option .= "<option>$val</option>";
}
print_r($option);
输出为:
<option>Name1</option>
<option>othername</option>
但是当我尝试申请我的数组时:view-source:http://vilavaleaprahovei.ro/kimea/allMarks.php与上面的$ array看起来相似的键结构:
$strarray = file_get_contents('allMarks.php');
$output = array_keys($strarray);
$option = '';
foreach ($output as $val) {
$option .= "<option>$val</option>";
}
print_r($option);
我收到两个错误:“ array_keys()期望参数1为数组”和“为foreach()提供了无效参数”。
请问我如何将allMarks.php数组键输出到选项标签中?
答案 0 :(得分:0)
获取数据
您应该包含/获取文件,但必须首先使其成为正确的PHP文件。
$array = require $pathtoFile;
正确的PHP文件是什么意思。好吧,它没有php标签<?php
,它应该return
数组,并且不要在末尾获取;
。当您打开文件时,它应该看起来像这样(除非它显然没有.....
):
<?php
return array( ..... ); //don't forget the semi-colon
然后它将起作用。如果您的服务器上没有禁用eval
,也可以使用file_put_contents
,但这会带来各种安全风险。因此,我什至不希望参与该讨论。
保存数据
使用var_export
和file_put_contents($pathtofile, '<?php return '.var_export($array, true).';');
保存这样的文件时,您应该这样:
file_put_contents($pathtofile, '<?php return '.file_get_contents($pathtofile).';');
然后它将包含使它们在语法上正确的这些内容。
修正您现在拥有的东西
如果您懒得(像我一样)手动编辑它,则可以执行以下操作:
<?php return <?php return array(...);;
只需确保您只对文件执行一次 ,否则您将得到诸如$jante = "http://vilavaleaprahovei.ro/kimea/feeds/alcarRO_wheels_feed.xml";
$xml=simplexml_load_file($jante);
$items = [];
foreach($xml->Produs as $child)
{
$marca = (string)$child->Marca;
if(!isset($items[$marca])) {
$items[$marca] = [];
}
$items[$marca]['diametru'][] = (int)$child->Diametru;
$items[$marca]['latime'][] = (int)$child->Latime;
$items[$marca]['pcd'][] = (int)$child->PCD;
$items[$marca]['pcd1'][] = (int)$child->PCD1;
$items[$marca]['et'][] = (int)$child->ET;
$items[$marca]['cb'][] = (int)$child->CB;
}
foreach($items as $marca => $item) {
///$myfile = fopen("allMarks.php", "w") or die("Unable to open file!"); //not needed
$items[$marca]['diametru'] = array_unique($items[$marca]['diametru']);
$items[$marca]['latime'] = array_unique($items[$marca]['latime']);
$items[$marca]['pcd'] = array_unique($items[$marca]['diametru']);
$items[$marca]['pcd1'] = array_unique($items[$marca]['diametru']);
$items[$marca]['et'] = array_unique($items[$marca]['diametru']);
$items[$marca]['cb'] = array_unique($items[$marca]['diametru']);
//fwrite($myfile, var_export($items, true)); //not needed
/* -NOTE-
This file is overwritten here on each iteration of the loop, you don't
see it because you are writing the top level $items, and then
progressively overwriting it and ending with the last (fully populated)
array in the file, but it wastes a lot of resources for nothing.
*/
//fclose($myfile); //not needed
}
$myfile = 'allMarks.php'; //not sure where this was set.
//add this
file_put_contents($myfile, '<?php return '.var_export($items, true).';');
//$strarray = file_get_contents('allMarks.php');
// var_dump($strarray);
之类的东西,当包含该文件时,它们只会被炸掉。 / p>
一旦正确获取了文件的语法,就可以如上所示包含它(就像任何PHP文件一样)。请记住,它必须是有效的PHP文件。
享受!
更新
这也许会清除所有东西:
$array = require 'allMarks.php';
//return $array; //not needed
//file_put_contents('allMarks.php', '<?php return '.file_get_contents('allMarks.php').';'); //not needed
$output = array_keys($array);
$option = '';
foreach ($output as $val) {
$option .= "<option>$val</option>";
}
print_r($option);
然后我尝试在html select选项中仅显示该数组的键:
//file_put_contents($myfile, '<?php return '.var_export($items, true).';');
//$array = require 'allMarks.php';
//just use the data
$output = array_keys($items);
请注意,如果该文件位于同一文件或同一请求中,则将其保存到文件中毫无意义,因为您已经拥有了数据,可以简单地使用它。如果需要跨请求的持久性,则只需将其存储在某个位置。如出于缓存原因。您读取数据后将创建文件,然后对此的下一个请求只需要读取您创建的文件,而不创建它:
foreach($items as $marca => $item) {
///$myfile = fopen("allMarks.php", "w") or die("Unable to open file!"); //not needed
$items[$marca]['diametru'] = array_unique($items[$marca]['diametru']);
$items[$marca]['latime'] = array_unique($items[$marca]['latime']);
$items[$marca]['pcd'] = array_unique($items[$marca]['diametru']);
$items[$marca]['pcd1'] = array_unique($items[$marca]['diametru']);
$items[$marca]['et'] = array_unique($items[$marca]['diametru']);
$items[$marca]['cb'] = array_unique($items[$marca]['diametru']);
//fwrite($myfile, var_export($items, true)); //not needed
//fclose($myfile); //not needed
}
您还可以消除整个循环:
$items[$marca]['{...}']
通过在foreach($xml->Produs as $child)
{
$marca = (string)$child->Marca;
if(!isset($items[$marca])) {
$items[$marca] = [];
}
$diametru = (int)$child->Diametru;
$items[$marca]['diametru'][$diametru] = $diametru; //save as key and value
$latime = (int)$child->Latime;
$items[$marca]['latime'][$latime] = $latime;
$pcd = (int)$child->PCD;
$items[$marca]['pcd'][$pcd] = $pcd;
$PCD1 = (int)$child->PCD1;
$items[$marca]['pcd1'][$PCD1] = $PCD1;
$et = (int)$child->ET;
$items[$marca]['et'][$et] = $et;
$cb = (int)$child->CB;
$items[$marca]['cb'][$cb] = $cb;
}
中设置键,PHP中的数组键是唯一的(您不能拥有两个具有相同值的键),因此您可以通过使用它们来利用这一点,并且避免进行另一个循环来调用唯一的数组他们。
在那之前的循环中:
array_keys
如果需要编号,您已经知道怎么做array_values
(可以使用相反的 $items[$marca]['diametru'][$x=(int)$child->Diametru] = $x;
$items[$marca]['latime'][$x=(int)$latime] = $x;
//..... etc
删除它)。而且,只需对每个对象执行此操作,就可以更轻松地设置它们:
$child's
但是有点难以理解,所以我不想让您感到困惑。基本上,我们将$x
属性的值分配给已设置的=
(分配了$items[$marca]['diametru']["value"] = "value"
单等号),然后可以将其用于键和值。然后,我们在下一行对其进行回收。
无论如何,您最终都将得到这个$items[$marca]['diametru'][0] = "value"
而不是array_values
,该值既是值又是键。您可以使用 //your original array (with the 2 loops) would look something like this,
//with numeric keys
$items[$marca]['diametru'] = [
0 => "value0",
1 => "value1",
2 => "value1", //duplicate
];
//by removing the second loop and using the value for both the key and value
$items[$marca]['diametru'] = [
"value0" => "value0",
"value1" => "value1",
//no duplicates possible as the keys are unique
];
将其删除(我不知道任何这些变量的值,因此我必须进行填充):
XML
现在我要强调的是,您不必删除密钥,在大多数情况下,密钥是否存在并不重要,只有当您特别需要对它们进行编号时,才需要循环回去并删除它们。即使这样,您仍将遍历较小的数组,因此仍可以节省一些性能(取决于重复项的数量,重复项越多,您保存的越多)。
看到这是共享代码的好处。通常有更高效,更轻松的方法来做事,像我这样的懒惰程序员,不需要做更多的工作即可。
这是我最好的解决方法,但看不到array_keys($items)
,但是您仍然会遇到问题,因为每次循环时都覆盖了该文件,因此我不确定到目前为止会进行哪些效果修复随着数组结构的发展。现在,如果您执行$marca
,它将为您提供$items[$marca]
的列表。除非您正在进行更多操作,否则这将使您正在做的事情变得多余。因为如果您需要的只是那个,那么您就不需要在数组{{1}}
干杯!