使用 PHP 对从 CSV 数据构建的多维数组进行排序

发布于 2024-10-06 15:38:25 字数 719 浏览 3 评论 0原文

尝试对 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 技术交流群。

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

发布评论

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

评论(4

給妳壹絲溫柔 2024-10-13 15:38:25

我最近做了这个。我有一个来自数据库的多维记录数组,我需要根据数组中的一个特定列对它们进行排序。这就是我所做的:

foreach($TimeRecords as $key => $value)
{
   $Rates[$key] = $value['rate'];
}
array_multisort($Rates, SORT_ASC, $TimeRecords);

我构建一个仅包含我需要的列的数组,然后使用 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:

foreach($TimeRecords as $key => $value)
{
   $Rates[$key] = $value['rate'];
}
array_multisort($Rates, SORT_ASC, $TimeRecords);

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.

情绪操控生活 2024-10-13 15:38:25

您可以考虑使用 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/

无名指的心愿 2024-10-13 15:38:25

您可能会发现 usort() 函数很有帮助。它接受回调参数,可用于按特定字段排序。

You may find usort() function helpful. It accepts callback argument, which may be used for sorting by specific field.

蹲墙角沉默 2024-10-13 15:38:25

我今天也遇到了类似的问题。基本上我最终要做的是创建一个临时表,将其加载到我需要的 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.

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