【问题标题】:Is this a array or not?这是一个数组吗?
【发布时间】:2019-04-05 12:09:26
【问题描述】:

我已经用数据库中的数据填充了这个数组

$collectTable1 = array( 'errand' => $interest->errand_id,
                        'timestamp' => $interest->timestamp,
                        'type' => $interest->type,
                        'amount' => $interest->amount
                    );

$collector[] = $collectTable1; 

我想重新排序时间戳,像这样

$sortTime = rsort($collectedData['timestamp']);

我试过了,我得到了这个输出

函数时间排序($a,$b){ 返回 (intval($a['timestamp']) > intval($b['timestamp'])); } usort($collector, "timesort");

2017-12-01 10:53:26

我认为我会从降序日期点得到?就像是 2018-09-04 12:32:16。

我的时间戳还包含这样的 unixtimestamp 和常规日期“ 2017-12-01 10:53:26"

【问题讨论】:

  • 使用rsort($collectedData) 因为$collectedData['timestamp'] 是单个值而不是数组,而不是$collectedData 是一个数组
  • 不知道,$collectedData 是什么?会不会是$collector的错字
  • 您可以使用is_array查看-secure.php.net/manual/en/function.is-array.php
  • 我想你可能的意思是你想按时间戳对数组进行排序,而不是像你试图做的那样......对时间戳进行排序
  • 可能您只保留了数据库中的一行数据,这看起来像是一个更大问题的症状。 I have this array filled with data from a database 当您获取数据时使用 fetchAll 或确保在“通常”一个 while 循环中使用 $collectTable1[] = $row 添加它或者仅使用 SQL 查询本身对数据进行排序(这不会解决“可能的”获取问题)

标签: php arrays sorting usort


【解决方案1】:

我猜你在$collector 中有一个元素数组。

如果您想按timestamp 对它们进行排序,您可以使用usort

考虑以下示例:

$collector = array();
$e1 = array("errand" => 1, "timestamp" => "2017-12-01 10:53:26");
$e2 = array("errand" => 2, "timestamp" => "2018-07-01 10:53:26");
$e3 = array("errand" => 3, "timestamp" => "2018-12-01 10:53:26");

$collector = array($e1, $e2, $e3);

function cmp($a, $b)
{
    return (strtotime($a['timestamp']) < strtotime($b['timestamp']));
}

usort($collector, "cmp");

当您的 timestamp 值在字符串中时,使用 strtotime 在比较之前将它们转换为 EPOC。

现在,$collector 数组元素按 timestamp 值排序。

代码示例的输出是:

Array
(
    [0] => Array
        (
            [errand] => 3
            [timestamp] => 2018-12-01 10:53:26
        )
    [1] => Array
        (
            [errand] => 2
            [timestamp] => 2018-07-01 10:53:26
        )
    [2] => Array
        (
            [errand] => 1
            [timestamp] => 2017-12-01 10:53:26
        )
)

【讨论】:

  • 我认为他们想要对时间戳进行反向排序。
  • 不确定更新后的版本是否更好-您仍在使用strcmp()
  • 转储排序后的数组,让我们看看它做了什么。
  • 添加了输出并更新了没有strcmp的代码
【解决方案2】:

一旦你有了你的数组:

<?php

$data = [
    ['timestamp' => '100'],
    ['timestamp' => '300'],
    ['timestamp' => '200']
];

usort($data, function($a, $b) {
    return $b['timestamp'] <=> $a['timestamp'];
});

var_export($data);

输出:

array (
    0 => 
    array (
    'timestamp' => '300',
    ),
    1 => 
    array (
    'timestamp' => '200',
    ),
    2 => 
    array (
    'timestamp' => '100',
    ),
)

【讨论】:

  • @splash58,这都是答案的有用信息,因为 OP 或未来的读者可能有早期版本的 PHP(尽管我希望会改进)
【解决方案3】:

如果关联数组 $collectTable1 的所有键的值都比

foreach($interest as $i){
$collectTable1 = array( 'errand' => array($i->errand_id),
                        'timestamp' => array($i->timestamp),
                        'type' => array($i->type),
                        'amount' => array($i->amount)
                    );


rsort($collectTable1[‘timestamp’]);

这里 collectTable1 是一维数组的关联数组,即。

$collectTable1['timestamp'][0]=firstvalue $collectTable1['timestamp'][1]=secondvalue

等等

【讨论】:

  • 可以给我们演示一下吗?
  • 我假设 $interest 是一些查询的数据集,例如 $interest=“SELECT * FROMinterests_table” 或类似的东西
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-15
  • 1970-01-01
  • 1970-01-01
  • 2021-11-08
  • 1970-01-01
相关资源
最近更新 更多