我将CSV数据加载到多维数组中.这样,每个"行"都是一个记录,每个"列"包含相同类型的数据.我正在使用下面的函数加载我的CSV文件.
function f_parse_csv($file, $longest, $delimiter)
{
$mdarray = array();
$file = fopen($file, "r");
while ($line = fgetcsv($file, $longest, $delimiter))
{
array_push($mdarray, $line);
}
fclose($file);
return $mdarray;
}
Run Code Online (Sandbox Code Playgroud)
我需要能够指定要排序的列,以便重新排列行.其中一列包含格式为的日期信息,Y-m-d H:i:s我希望能够以最近的日期为第一行进行排序.
I've had trouble with the examples in the PHP manual, so I'd like to ask this here...
I have an array of objects.. Is there a way to sort it based on the contents of the object?
For example, my array is:
Array
(
[0] => stdClass Object
(
[id] => 123
[alias] => mike
)
[1] => stdClass Object
(
[id] => 456
[alias] => alice
)
[2] => stdClass Object
(
[id] => 789
[alias] => zeke
) …Run Code Online (Sandbox Code Playgroud) 我有一个像这样的数组:
Array
(
[0] => Array
(
[title] => some title
[time] => 1279231500
)
[1] => Array
(
[title] => some title 2
[time] => 1279231440
)
[2] => Array
(
[title] => some title 3
[time] => 1279229880
)
)
Run Code Online (Sandbox Code Playgroud)
我怎么能根据时间对它进行排序?
好的,我有一个用于传输名称的数组,它看起来像这样:
array(2) {
[0]=>
array(3) {
["firstName"]=>
string(3) "Joe"
["lastName"]=>
string(5) "Black"
["uid"]=>
int(3225)
}
[1]=>
array(3) {
["firstName"]=>
string(4) "John"
["lastName"]=>
string(3) "Doe"
["uid"]=>
int(3516)
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我该如何对这个数组进行排序lastName?
是的,我已经搜索并尝试了许多技术,但似乎没有任何工作.这是我的数组:
Array
(
[0] => stdClass Object
(
[id] => 119
[name] => Business3
[start_date] => 1338789600
[end_date] => 1354604400
)
[1] => stdClass Object
(
[id] => 153
[name] => Business1
[start_date] => 1338962400
[end_date] => 1370498400
)
[2] => stdClass Object
(
[id] => 135
[name] => Business2
[start_date] => 1339653600
[end_date] => 1356937200
)
)
Run Code Online (Sandbox Code Playgroud)
我基本上想要通过名称键对它进行排序,但我在Stackoverflow上尝试的每个函数似乎都不起作用,因为我得到一个没有错误的空白页面.
我试过这个:
function array_sort_by_column(&$arr, $col, $dir = SORT_ASC) {
$sort_col = array();
foreach ($arr as $key=> $row) {
$sort_col[$key] = $row[$col];
} …Run Code Online (Sandbox Code Playgroud) 我试图使用array_multisort()函数...... 在子数组的基础上对数组进行排序......
在尝试时;
print_r($mar); echo '<br>';
$arr2 = array_multisort($mar, array('wek'=>SORT_ASC));
print_r($arr2);
Run Code Online (Sandbox Code Playgroud)
得到错误 array_multisort(): Array sizes are inconsistent
排序前的输出是
Array (
[0] => Array ( [dat] => 1 [wek] => 5 [mac] => A100 [mcr] => #00c8ff )
[1] => Array ( [dat] => 2 [wek] => 9 [mac] => A100 [mcr] => #00c8ff )
[2] => Array ( [dat] => 5 [wek] => 13 [mac] => A100 [mcr] => #00c8ff )
[3] => Array ( [dat] => 5 …Run Code Online (Sandbox Code Playgroud) 这是我的数组,如何通过saleref对其进行排序?
Array
(
[xml] => Array
(
[sale] => Array
(
[0] => Array
(
[saleref] => 12345
[saleline] => 1
[product] => producta
[date] => 19/ 3/10
[manifest] => 0
[qty] => 1
[nextday] =>
[order_status] =>
)
[1] => Array
(
[saleref] => 12344
[saleline] => 1
[product] => productb
[date] => 18/ 3/10
[manifest] => 11892
[qty] => 1
[nextday] =>
[order_status] =>
)
Run Code Online (Sandbox Code Playgroud)