0

This array type of out I required.

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] => [email protected]
        )

    [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] => [email protected]
        )

)

I want to save all data using date as key and if one date has multiple array than merge data into one the same key. Key should not be different. And All other which having same date with not repeated values also should have save with same procedure. Below is code which I've written.

$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;

The code is returning only the data for repeated values and not returning data which are not repeating. Also keys are returning fine but the values are merging from another date. Any help will be appreciated.

Input Array Which we want to sort:

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] => [email protected]
    )

[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
    )
   )
4
  • Please provide expected output of data you pasted. Commented May 25, 2018 at 8:06
  • @Pyton please check edited code. Commented May 25, 2018 at 8:13
  • Ok but what is the input :) Commented May 25, 2018 at 8:14
  • added array at last Commented May 25, 2018 at 8:31

1 Answer 1

1

This should work:

$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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.