输出 CSV 到数组

发布于 2024-12-04 09:10:19 字数 1091 浏览 6 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

悲欢浪云 2024-12-11 09:10:19

在循环的每次迭代中,当您想要添加 $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.

盛夏已如深秋| 2024-12-11 09:10:19

您应该使用 fgetcsv() (docs) 函数,而不是试图理解您自己的 CSV 数据。另外,您应该在循环文件时构建 $items 数组(而不是覆盖它),每次添加一个新项目。

$file_handle = fopen('curriculum.csv', 'r');
fgets($file_handle); // this skips the csv header line
while (($parts = fgetcsv($file_handle)) !== FALSE) {
    $items[] = $parts[2] . ' ' . $parts[3];
}
fclose($file_handle);

foreach ($items as $item) {
    // Your HTML code goes here
    echo $item . PHP_EOL;
}

加分(使用对象和迭代器,你会看起来更酷!)

$file = new SplFileObject('curriculum.csv');
$file->setFlags(SplFileObject::READ_CSV);
foreach (new LimitIterator($file, 1) as $parts) {
    $items[] = $parts[2] . ' ' . $parts[3];
}

foreach ($items as $item) {
    // Your HTML code goes here
    echo $item . PHP_EOL;
}

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.

$file_handle = fopen('curriculum.csv', 'r');
fgets($file_handle); // this skips the csv header line
while (($parts = fgetcsv($file_handle)) !== FALSE) {
    $items[] = $parts[2] . ' ' . $parts[3];
}
fclose($file_handle);

foreach ($items as $item) {
    // Your HTML code goes here
    echo $item . PHP_EOL;
}

Bonus points (you'll look cooler, using objects and iterators!)

$file = new SplFileObject('curriculum.csv');
$file->setFlags(SplFileObject::READ_CSV);
foreach (new LimitIterator($file, 1) as $parts) {
    $items[] = $parts[2] . ' ' . $parts[3];
}

foreach ($items as $item) {
    // Your HTML code goes here
    echo $item . PHP_EOL;
}
离笑几人歌 2024-12-11 09:10:19

在你的情况下, items 应该是一个数组。在 while 循环之外初始化它

$items = array();

然后在 while 循环内部

$items[] = array ($parts[2] .$parts[3], );

应该可以工作。

另请参阅 fgetcsv 以及建议的内容。

items should be an array in your case. Initialize it outside the while loop

$items = array();

And then inside the while loop

$items[] = array ($parts[2] .$parts[3], );

should work.

Also look at fgetcsv as well as suggested.

︶葆Ⅱㄣ 2024-12-11 09:10:19

您的阵列需要一个额外的维度。

$i=0;
$items= new array(); // i think you need this for some fun reason
while (!feof($file_handle) ) {

    $line_of_text = fgets($file_handle);
    $parts = explode(',', $line_of_text);

    $items[$i] = array ($parts[2] .$parts[3], );

}

You need an extra dimenson to your array.

$i=0;
$items= new array(); // i think you need this for some fun reason
while (!feof($file_handle) ) {

    $line_of_text = fgets($file_handle);
    $parts = explode(',', $line_of_text);

    $items[$i] = array ($parts[2] .$parts[3], );

}
忆梦 2024-12-11 09:10:19

您正在覆盖 $items 数组。我想建议一种更简单的方法,

<code><pre><?php

    // in one step, load the datafile as an array, and loop over each line
    // then explode the line by comma, and add to $lines array
    foreach(file('curriculum.csv') as $line)
            $lines[] = explode(',',$line);

    // display the structure of the captured data
    print_r($lines);

?></pre></code>

我认为一旦您了解了此处捕获的数据的结构,您就可以在输出循环中根据需要使用这些值。

为了方便起见,添加了 codeprephp 标签

You are overwriting the $items array. I would like to suggest a much simpler method to

<code><pre><?php

    // in one step, load the datafile as an array, and loop over each line
    // then explode the line by comma, and add to $lines array
    foreach(file('curriculum.csv') as $line)
            $lines[] = explode(',',$line);

    // display the structure of the captured data
    print_r($lines);

?></pre></code>

I 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 and php tags added for convenience

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文