hello everyone

        [{
                "uid": "BR-M12068",
                "registeredDate": "Thursday May 11th 2017  03:56:37 PM",
                "file_path": "BR-M12068.pdf",
                "file_thumbnail_path": "BR-M12068thumb.png",
                "test_type": "Free",
                "title": "Medical Bills",
                "createdAt": "Thursday June 29th 2017 05:02:19",
                "chkTime": "Thursday June 29th 2017 05:01:53 PM",
                "chkoutTime": "Thursday June 29th 2017  05:02:29 PM"
            },
            {
                "uid": "BR-M12068", /// first data
                "registeredDate": "Thursday May 11th 2017  03:56:37 PM",
                "file_path": "BR-M12068.pdf",
                "file_thumbnail_path": "CEthumb.png",
                "test_type": "not-free",
                "title": "Medical Bills",
                "createdAt": "Sunday June 25th 2017 02:29:38",
                "chkTime": "Sunday June 25th 2017 02:28:22 PM",
                "chkoutTime": "Sunday June 25th 2017  02:30:07 PM"
            },
            {
                "uid": "BR-M12068", //second data
                "registeredDate": "Thursday May 11th 2017  03:56:37 PM",
                "file_path": "BR-M12068.pdf",
                "file_thumbnail_path": "BRthumb.png",
                "test_type": "Free",
                "title": "Medical Bills",
                "createdAt": "Sunday June 25th 2017 02:29:13",
                "chkTime": "Sunday June 25th 2017 02:28:22 PM",
                "chkoutTime": "Sunday June 25th 2017  02:30:07 PM"
            }
        ]

    i have json data like above as we can see createdAt is same for two Sunday June 25th 2017 02:29:13  

    now i am trying two merge common date in associative array,but there is no success please help 

    i want out like this 

    date-1 [
            "uid": "BR-M12068",
                        "registeredDate": "Thursday May 11th 2017  03:56:37 PM",
                        "file_path": "BR-M12068.pdf",
                        "file_thumbnail_path": "BR-M12068thumb.png",
                        "test_type": "Free",
                        "title": "Medical Bills",
                        "createdAt": "Thursday June 29th 2017 05:02:19",
                        "chkTime": "Thursday June 29th 2017 05:01:53 PM",
                        "chkoutTime": "Thursday June 29th 2017  05:02:29 PM"

    ]

    date -2[which is same] [

                                [0] -----> first data

                                [1] ----->second data 

    ]

Recommended Answers

All 6 Replies

Member Avatar for diafol

That date format is yucky. I'd json_decode it as an associative array then loop through creating keys from the date but only after changing the format to Y-m-d via Datetime obj. You then add the rest of the array as an element of that key. If I have time I'll post an example. You may be able to use array functions too, but these tend to be slower even though they're more succinct.

yes please,if you have time post example :) thanks in advance

Member Avatar for diafol

You do realise that this:

i have json data like above as we can see createdAt is same for two Sunday June 25th 2017 02:29:13

isn't true. Your createdAt times are all unique - which you would expect if they are all created individually.

"createdAt": "Sunday June 25th 2017 02:29:38", //second record
"createdAt": "Sunday June 25th 2017 02:29:13", //third record
Member Avatar for diafol

This function will group output:

function jsonToDateKeyArray( $json, $dateKey, $keyFormat='YmdHis' )
{
    $array = json_decode($json, true);
    $container = [];
    foreach($array as $r){
        $dt = new DateTime($r[$dateKey]);
        $key = $dt->format($keyFormat);
        $container[$key][] = $r;
    }
    return $container;
}

echo "<pre>";
print_r(jsonToDateKeyArray($json, 'createdAt'));

However all your createdAtfields are unique, so they will show as three elements for the json you displayed, not the two you are expecting.

Member Avatar for diafol

So, did it work?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.