在关联数组中为重复值和单个值创建键

时间:2018-05-25 08:02:40

标签: php arrays sorting

我需要的这种数组类型。

Array
(
[2018-03-25] => Array
(
    [0] => Array
        (
            [loyalty_transcation_id] => 33
            [user_id] => 58
            [bill_copy_image_path] => 03252018_182712.jpg
            [create_date] => 2018-03-25 19:27:12
            [name] => bernd koch
        )

    [1] => Array
        (
            [loyalty_transcation_id] => 3
            [user_id] => 21
            [bill_copy_image_path] => 09132017_044456.jpg
            [create_date] => 2018-03-25 05:44:56
            [name] => Rahul Upadhyay
        )

)

[2017-09-27] => Array

(
    [0] => Array
        (
            [loyalty_transcation_id] => 10
            [user_id] => 24
            [bill_copy_image_path] => 09272017_115913.jpg
            [create_date] => 2017-09-27 12:59:13
            [name] => raj@ot.com
        )

    [1] => Array
        (
            [loyalty_transcation_id] => 3
            [user_id] => 21
            [bill_copy_image_path] => 09132017_044456.jpg
            [create_date] => 2017-09-27 05:44:56
            [name] => Rahul Upadhyay
        )

)

[2017-09-28] => Array

(
    [0] => Array
        (
            [loyalty_transcation_id] => 10
            [user_id] => 24
            [bill_copy_image_path] => 09272017_115913.jpg
            [create_date] => 2017-09-28 12:59:13
            [name] => raj@ot.com
        )

)

我想使用date作为密钥保存所有数据,如果一个日期有多个数组而不是将数据合并到一个相同的密钥中。关键不应该是不同的。并且具有相同日期且没有重复值的所有其他也应该用相同的程序保存。以下是我写的代码。

$dups = $new_arr = array();
            foreach ($query->rows as $key => $val) {
                $new = date('Y-m-d', strtotime($val['create_date']));

                if (!isset($new_arr[$new])) {
                    $new_arr[$new] = $val;

                    //die;
                } else {
                    if (isset($dups[$new])) {
                       $dups[$new][] = $val;
                       //print_r($dups[$new]);
                      //echo "\n";
                    } else {
                       //$dups[$new] = array($val);
                       $dups[$new] = array($new_arr[$new], $value);
                    }
                }
            }
            //echo "<pre>";
            print_R($dups);
            die;

代码仅返回重复值的数据,而不返回不重复的数据。键也正常返回,但值正在从另一个日期合并。任何帮助将不胜感激。

输入数组我们想要排序:

Array
(
[0] => Array
    (
        [loyalty_transcation_id] => 36
        [user_id] => 23
        [bill_copy_image_path] => 05082018_144348.jpg
        [create_date] => 2018-05-08 15:43:48
        [name] => Admin
    )

[1] => Array
    (
        [loyalty_transcation_id] => 35
        [user_id] => 18
        [bill_copy_image_path] => 04052018_160009.jpg
        [create_date] => 2018-04-05 17:00:09
        [name] => gurpreet.singh@outlook.de
    )

[2] => Array
    (
        [loyalty_transcation_id] => 33
        [user_id] => 58
        [bill_copy_image_path] => 03252018_182712.jpg
        [create_date] => 2018-03-25 19:27:12
        [name] => bernd koch
    )

[3] => Array
    (
        [loyalty_transcation_id] => 32
        [user_id] => 57
        [bill_copy_image_path] => 03252018_160706.jpg
        [create_date] => 2018-03-25 17:07:06
        [name] => alex
    )
   )

1 个答案:

答案 0 :(得分:1)

这应该有效:

$responseArr = array();
foreach($inputArr as $input) {
    $dateKey = date('Y-m-d', strtotime($input['create_date']));
    if(!isset($responseArr[$dateKey])) {
        $responseArr[$dateKey] = array();
    }
    array_push($responseArr[$dateKey], $input);
}