【问题标题】:How to create an array from the existing array on basis of key and value match [closed]如何根据键和值匹配从现有数组创建数组[关闭]
【发布时间】:2015-07-06 11:40:06
【问题描述】:

我有一个数组我希望属于同一天的数据将出现在一个索引上,如下面的数组我在 1 和 2 索引中的日期相同,所以结果数组应该像我的 ewault 数组这样数据属于同一日期的将显示在同一索引上。我该怎么做?

我的数组:

Array
(
    [0] => Array
        (
            [pkMessageID] => 6
            [fkPostID] => 4
            [messageSenderID] => 2
            [messageRecieverID] => 19
            [messageBody] => Nice
            [messageStatus] => 1
            [DateOnly] => 2015-07-07
            [messageDateAdded] => 2015-07-07 16:20:58
        )

    [1] => Array
        (
            [pkMessageID] => 5
            [fkPostID] => 4
            [messageSenderID] => 19
            [messageRecieverID] => 2
            [messageBody] => Hi I am good how r u
            [messageStatus] => 1
            [DateOnly] => 2015-07-06
            [messageDateAdded] => 2015-07-06 16:14:05
        )

    [2] => Array
        (
            [pkMessageID] => 4
            [fkPostID] => 4
            [messageSenderID] => 2
            [messageRecieverID] => 19
            [messageBody] => Hello akhilesh how r u
            [messageStatus] => 1
            [DateOnly] => 2015-07-06
            [messageDateAdded] => 2015-07-06 16:12:22
        )

)

但是,结果数组应该是:

Array
(
    [0] => Array
        (
            [pkMessageID] => 6
            [fkPostID] => 4
            [messageSenderID] => 2
            [messageRecieverID] => 19
            [messageBody] => Nice
            [messageStatus] => 1
            [DateOnly] => 2015-07-07
            [messageDateAdded] => 2015-07-07 16:20:58
        )

    [1] => Array
        (
 [0] => Array
        (
            [pkMessageID] => 5
            [fkPostID] => 4
            [messageSenderID] => 19
            [messageRecieverID] => 2
            [messageBody] => Hi I am good how r u
            [messageStatus] => 1
            [DateOnly] => 2015-07-06
            [messageDateAdded] => 2015-07-06 16:14:05
        )

    [1] => Array
        (
            [pkMessageID] => 4
            [fkPostID] => 4
            [messageSenderID] => 2
            [messageRecieverID] => 19
            [messageBody] => Hello akhilesh how r u
            [messageStatus] => 1
            [DateOnly] => 2015-07-06
            [messageDateAdded] => 2015-07-06 16:12:22
        )
)

)

【问题讨论】:

  • 你有试过的代码吗?
  • 我很难弄清楚你想要实现什么。您是说要根据 messageDateAdded 对数组的第 2 级进行分组吗?另外,到目前为止,您尝试过什么?
  • @mituw16 是的,我希望属于添加的同一日期的数据应该进入一个级别,就像我在 2015 年 7 月 7 日添加的结果数组数据中的一个索引和 2015 年 7 月添加的数据一样6 将出现在不同的索引上

标签: php arrays


【解决方案1】:

这可以通过以下方式完成:

$out = array();

foreach($input as $data)
{
    if( ! is_array($out[$data['DateOnly']])){
        $out[$data['DateOnly']] = array();
    }
    $out[$data['DateOnly']][] = $data;
}

我没有代码,但我希望你能明白。只需将日期用作数组中的索引。所以结果会是这样的。

[2015-7-6] => Array ( [0] => Array( DateOnly => 2015-7-6, ..))
[2015-7-7] => Array ( [0] => Array( DateOnly => 2015-7-7, .. ), [1] => Array(DateOnly => 2015-7-7 , .. ))

【讨论】:

  • 这是工作代码 $out = array(); foreach($arrMessage as $data) { // echo '
    ';print_r($data['DateOnly']);die; if( is_array($data['DateOnly'])){ $out[$data['DateOnly']] = array(); } $out[$data['DateOnly']][] = $data; }
猜你喜欢
  • 2018-09-08
  • 2019-12-15
  • 1970-01-01
  • 2021-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-27
相关资源
最近更新 更多