Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按列对平面文件中的数据进行排序_Php_Arrays_Sorting_Netcat - Fatal编程技术网

Php 按列对平面文件中的数据进行排序

Php 按列对平面文件中的数据进行排序,php,arrays,sorting,netcat,Php,Arrays,Sorting,Netcat,我以以下方式将数据存储在平面文件中: 1|$item|$price|$quality|$keywords 实际上是这样的: 1|Shoes|50|5|Classy,basic 3|Shirt|25|4|Simple 2|Tshirt|20|1|Basic, urban 4|Coat|45|3|Classy, preppy 我试图按我选择的一列对数据进行排序,比如说按价格。以下是ajax函数调用的文件,其中列出了数据: $cat_file = "data.php"; $cat_db = fil

我以以下方式将数据存储在平面文件中:

1|$item|$price|$quality|$keywords
实际上是这样的:

1|Shoes|50|5|Classy,basic
3|Shirt|25|4|Simple
2|Tshirt|20|1|Basic, urban
4|Coat|45|3|Classy, preppy
我试图按我选择的一列对数据进行排序,比如说按价格。以下是ajax函数调用的文件,其中列出了数据:

$cat_file = "data.php";
$cat_db = file("$cat_file");
$call = fopen("$cat_file","r");
foreach($cat_db as $cat_line) {
$cat_line_arr = explode("|",$cat_line);
$cat_line_id = $cat_line_arr[1];
    if($cat_line_id == $cat) {

       $id = $cat_line_arr[0];
       $item = $cat_line_arr[1];
       $price = $cat_line_arr[2];
       $quality = $cat_line_arr[3];
       $keywords = $cat_line_arr[4];

        if (BLABLABLACONDITION) {

        *LISTS THE DATA SORTED THE WAY I WANT*

        }
    }
}
fclose($call);
我上面的代码工作得很好,但是没有具体的排序,它只是按照文件的原始顺序列出数据。 如果我按价格和降序排序,下面是我想要得到的结果,例如:

1|Shoes|50|5|Classy,basic
4|Coat|45|3|Classy, preppy
3|Shirt|25|4|Simple
2|Tshirt|20|1|Basic, urban

我应该替换什么列出按我希望的方式排序的数据?提前感谢您的帮助

我不确定我是否完全理解您的问题,所以在这里猜猜,您是否在寻找krsort()和ksort()

等等

ksort($cat_line_price);
按价格升序重新排列数组

krsort($cat_line_price);
// read file as string and turn into an array, 1 element per line
$records = explode("\n", file_get_contents('path/to/file'));

// turn each line into an array, 1 element per field
array_walk($records, function (&$record) { $record = explode('|', $record); });

//$records is now a two-dimensional array containing the data

// gather the prices into a new array
$prices = array_map(function ($record) { return (int) $record[2]; }, $records);

// use the prices to sort the records
array_multisort($prices, SORT_ASC, $records);

foreach ($records as $record) {
   //... do your thing.
}

根据价格按降序重新排列阵列

看起来像我要找的!我试图按照我选择的某一列对数组进行排序,例如,通过保持与行其余部分的相关性,按升序排列价格。我试着在我的代码中像这样使用你的代码片段,但我得到了一个错误(ksort期望参数等),我应该如何实现这一点才能工作?很抱歉回复太晚,去购物了,这是一个来自w3schools的示例,谢谢你的帮助;我看了一下,我的问题是我不明白我的数据是如何转化为每个教程显示的数组结构的;据我所知,它们都有2列,而我有很多列,并且希望按照第一列以外的另一列进行排序(这就是为什么ksort会这样做?)。我想这一次我在试图解决一些比我的编码水平更高的问题。事实上,OscarJ给出了正确的答案。您正在处理多维数组(OscarJ分解为多维数组)。只需查找他在示例中使用的每个php函数。由于每个项都使用一行,因此他使用explode(),每行(\n)作为文件集上的分隔符,该文件集位于file_get_contents('path/to/file')中,它将文件作为字符串获取。然后,OscarJ使用array_walk()在函数中运行数组的每个元素(记录中的每一行)。他在这个函数中所做的是将($records数组)的每个元素分解成其他数组,使用|作为分隔符。然后,使用数组映射将主数组($records)中每个数组($record)的每个值发送给选择$record[2](价格)的函数,从而创建一个新的多维数组,其中每个子数组的键现在是价格(而不是原始文件中的ID).谢谢你的帮助,Kobbe向我解释了你的代码,我会仔细看看的!=谢谢你。我会添加一些注释,让它更容易理解。非常感谢!这非常有帮助!
krsort($cat_line_price);
// read file as string and turn into an array, 1 element per line
$records = explode("\n", file_get_contents('path/to/file'));

// turn each line into an array, 1 element per field
array_walk($records, function (&$record) { $record = explode('|', $record); });

//$records is now a two-dimensional array containing the data

// gather the prices into a new array
$prices = array_map(function ($record) { return (int) $record[2]; }, $records);

// use the prices to sort the records
array_multisort($prices, SORT_ASC, $records);

foreach ($records as $record) {
   //... do your thing.
}