Thursday, 30 August 2018

Create a PHP array with the same key for multiple values

I'm posting a question already made here before and was not properly cleared for me.

I am trying to adapt a simple jquery code called “flexible calendar” and fill events trough our admin with meta keys.
The key is a date, the value a string. Here comes the problem. In order to use a jQuery calendar, I can dynamically create a JSON object like this:
var codropsEvents = {
'11-23-2016' : 'text',
'11-23-2016' : 'text two',
'11-20-2016' : 'some other text',
'11-19-2016' : 'Anything that is text'
};

But I need to get a JSON object as follows :
var codropsEvents = {
'11-23-2016' : 'text', 'text two',
'11-20-2016' : 'some other text',
'11-19-2016' : 'Anything that is text'
};

In this case, I need a code that causes them to recognize same date entries, and the nest correspontes values to the events of the same day, on the same row.
The thing is, PHP doesn't allow to use the same key in multiple rows : if I have multiple posts with the same date (a wp meta value), PHP only stores one in the array.
My calendar displays only one post per day.
How can I organize my PHP array in order to get the good format of the JSON object?
In fact, I don't need multiple rows with the same key. For every post with the same date, I need to create a row with a unique key (the date) and a unique value which will be a string containing all the posts titles.

You can do like this;
JSON
var codropsEvents = {
"11-23-2015":[
   "text",
   "some text",
   "more text",
   "bla bla"]
}

PHP
$codropsEvents  = array('11-23-2015'=> array('text','some text','more text','bla bla'))

0 comments:

Post a Comment