使用 PHP 对从 CSV 数据构建的多维数组进行排序
尝试对 PHP 中从 CSV 填充的数组进行排序。理想情况下,我也希望能够通过单击此处表格中的选项卡来控制排序。不过现在,我手头的第一个任务就是整理该死的东西..已经为此工作了3天多了..非常感谢任何帮助!干杯!
PHP
<?php
$fp = fopen("https://spreadsheets.google.com/pub?key=0AjZgwY03sLMGdHVoWjhucGowWWJBb2g2NnQzVG9HZFE&hl=en&single=true&gid=0&output=csv","r");
$rows = array();
while (($row = fgetcsv($fp)) !== FALSE) {
$rows[] = $row;
}
fclose($fp);
$headers = array_shift($rows);
foreach ($rows as $row) : list($ShowKey, $ShowFeedURL, $ShowLink, $ShowIcon, $ShowTitle, $ShowTag, $ShowCategory, $ShowEps, $ShowLastUpdate, $ShowNew) = $row;
$oddpost = ( empty( $oddpost ) ) ? '_odd' : ''; ?>
Trying to sort an array in PHP that is being populated from a CSV. I would also, ideally, LOVE to be able to control the sort by clicking on tabs in the table here .. Right now, though, my first task at hand is just sorting the damn thing.. been working on this for over 3 days now.. any help is GREATLY appreciated!! Cheers!
PHP
<?php
$fp = fopen("https://spreadsheets.google.com/pub?key=0AjZgwY03sLMGdHVoWjhucGowWWJBb2g2NnQzVG9HZFE&hl=en&single=true&gid=0&output=csv","r");
$rows = array();
while (($row = fgetcsv($fp)) !== FALSE) {
$rows[] = $row;
}
fclose($fp);
$headers = array_shift($rows);
foreach ($rows as $row) : list($ShowKey, $ShowFeedURL, $ShowLink, $ShowIcon, $ShowTitle, $ShowTag, $ShowCategory, $ShowEps, $ShowLastUpdate, $ShowNew) = $row;
$oddpost = ( empty( $oddpost ) ) ? '_odd' : ''; ?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我最近做了这个。我有一个来自数据库的多维记录数组,我需要根据数组中的一个特定列对它们进行排序。这就是我所做的:
我构建一个仅包含我需要的列的数组,然后使用 array_multisort() 函数根据该列对数组进行排序。
您可以在 PHP 中编写执行此操作的函数,然后只需使用 javascript ajax 调用来调用它们,并在完成排序后重新加载页面的该部分。
I recently did this. I had a multi-dimensional array of records from a database, and I needed to sort them based off of one specific column in the array. Here's what I did:
I build an array of only the column I need, then I use the array_multisort() function to sort the array based off of that column.
You can write functions that will do this in PHP and then just call them with javascript ajax calls and reload that part of the page when it's done sorting.
您可以考虑使用 Javascript 在客户端进行排序,而不是在 PHP 中对表进行排序。例如,看看这个 jQuery 插件: http://tablesorter.com/
Instead of sorting the table in PHP, you may consider doing it client-side in Javascript. For instance have a look at this jQuery plugin: http://tablesorter.com/
您可能会发现 usort() 函数很有帮助。它接受回调参数,可用于按特定字段排序。
You may find usort() function helpful. It accepts callback argument, which may be used for sorting by specific field.
我今天也遇到了类似的问题。基本上我最终要做的是创建一个临时表,将其加载到我需要的 csv 文件的行中。从那里,我使用 php 对数据进行排序和组织,并更新或添加到我需要更改的表中。
例如,我创建了一个名为“temp”的表,并加载到我需要的类别的所有行中。然后,一旦将其放入表中,我就编写了一个 php 脚本,该脚本按总销售额降序对信息进行排序。从那里,我执行了一个查询来更新我的主表,并使用限制来控制它(仅按销售数量排名前 200 件商品)。
这很容易做到,希望它会对您或其他人有所帮助。
记住。如果您要多次执行此操作,则需要先截断临时表以删除旧行。
I had a similar issue to this today. Basically what I ended up doing is creating a temporary table which I loaded in the rows from the csv file that I needed. From there, I used php to sort and organize the data and update or add to the table I needed to alter.
Example, I made a table called 'temp' and loaded in all the rows of the category I needed. Then, once this was in the table, I made a php script which sorted the information by number of total sales in descending order. From there, I did a query to update my main table and used a limit to control this (only the top 200 items by number of sales).
It was very easy to do and hopefully it will help you or someone else.
Keep in mind. If you're going to do this more than once, you will need to truncate the temporary table to remove the old rows first.