输出 CSV 到数组
我正在使用 WP Alchemy 创建一个带有多个复选框的元框。
我遇到的问题是,我有一个 csv 学术课程列表,我想从中创建复选框选项。所以我将 csv 转换成一个数组,盒子是由数组组成的。但是,使用我现在的代码,仅在数组中创建最后一行。
<?php
$file_handle = fopen('curriculum.csv', 'r');
while (!feof($file_handle) ) {
$line_of_text = fgets($file_handle);
$parts = explode(',', $line_of_text);
$items = array ($parts[2] .$parts[3], );
}
fclose($file_handle);
?>
<?php foreach ($items as $i => $item): ?>
<?php $mb->the_field('cb_ex3'); ?>
<!-- similar to test #2, the same thing can be accomplished by simply
adding array brackets "[]" to the name -->
<input type="checkbox" name="<?php $mb->the_name(); ?>[]" value="<?php echo $item; ?>"<?php $mb->the_checkbox_state($item); ?>/> <?php echo $item; ?><br/>
<?php endforeach; ?>
因此,在几百行代码中,我得到的只是最后一行。 这是我正在使用的实际文件: http://www .ouhsd.k12.ca.us/educational_services/curriculum/curriculum.csv
I am using WP Alchemy to create a meta box with multiple check boxes.
The trouble I'm having is, I have a csv list of academic courses that I want to create the check box options from. So I'm taking the csv turning it into an array that the boxes are made from. However with the code I have now, only the LAST line is created in the array.
<?php
$file_handle = fopen('curriculum.csv', 'r');
while (!feof($file_handle) ) {
$line_of_text = fgets($file_handle);
$parts = explode(',', $line_of_text);
$items = array ($parts[2] .$parts[3], );
}
fclose($file_handle);
?>
<?php foreach ($items as $i => $item): ?>
<?php $mb->the_field('cb_ex3'); ?>
<!-- similar to test #2, the same thing can be accomplished by simply
adding array brackets "[]" to the name -->
<input type="checkbox" name="<?php $mb->the_name(); ?>[]" value="<?php echo $item; ?>"<?php $mb->the_checkbox_state($item); ?>/> <?php echo $item; ?><br/>
<?php endforeach; ?>
So out of the few hundred lines of code, all I get is the last line.
Here is the actually file I'm working with: http://www.ouhsd.k12.ca.us/educational_services/curriculum/curriculum.csv
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在循环的每次迭代中,当您想要添加 $items 时,您会替换它,因此它只有最后一个值。
另外 http://php.net/fgetcsv 可能对您有用。
In each iteration of the loop you replace $items when you want to add to it, thus it only having the last value.
Also http://php.net/fgetcsv might be of some use to you.
您应该使用
fgetcsv()
(docs) 函数,而不是试图理解您自己的 CSV 数据。另外,您应该在循环文件时构建$items
数组(而不是覆盖它),每次添加一个新项目。加分(使用对象和迭代器,你会看起来更酷!)
You should be using the
fgetcsv()
(docs) function rather than trying to make sense of the CSV data yourself. Also, you should build the$items
array (rather than overwriting it) as you loop over the file, adding a new item each time.Bonus points (you'll look cooler, using objects and iterators!)
在你的情况下, items 应该是一个数组。在 while 循环之外初始化它
然后在 while 循环内部
应该可以工作。
另请参阅 fgetcsv 以及建议的内容。
items should be an array in your case. Initialize it outside the while loop
And then inside the while loop
should work.
Also look at fgetcsv as well as suggested.
您的阵列需要一个额外的维度。
You need an extra dimenson to your array.
您正在覆盖
$items
数组。我想建议一种更简单的方法,我认为一旦您了解了此处捕获的数据的结构,您就可以在输出循环中根据需要使用这些值。
为了方便起见,添加了
code
、pre
和php
标签You are overwriting the
$items
array. I would like to suggest a much simpler method toI think once you understand the structure of the captured data here, you can use the values as you wish in your output loop.
code
,pre
andphp
tags added for convenience