0

In wordpress, I call a WP_Query. For each post found, I add a key+value row to a PHP array.

$array[$key] = $value;

The key is a timestamp, the value a string. Here comes the problem. In order to use a jQuery calendar, I need to get a JSON object as follows :

var codropsEvents = {
'11-23-2015' : 'text',
'11-23-2015' : 'text two',
'11-20-2015' : 'some other text',
'11-19-2015' : 'Anything that is text'
};

To do this, the json_encode() function almost works.

The thing is, PHP doesn't allow to use the same key in multiple rows : if I have multiple posts with the same timestamp (a wp meta value), PHP only stores one in the array.

My calendar therefore displays only one post per day.

How can I organize my PHP array in order to get the good format of the JSON object?

** /!\ UPDATE /!\ **

In fact, I don't need multiple rows with the same key. For every post with the same timestamp, I need to create a row with a unique key (the timestamp) and a unique value which will be a string containing all the posts titles.

Hope I'm clear.

8
  • did you try to add each post as a subarray? Commented Aug 7, 2015 at 12:15
  • Not yet, I'm trying it right now. Commented Aug 7, 2015 at 12:17
  • add the post ID to the date string as a suffix '11-23-2015_'+postID then remove it in php side Commented Aug 7, 2015 at 12:17
  • Okay, so I tried array_push($array, array($date => 'value')); But json_encode returns an object of multiple objects containing one row each. Commented Aug 7, 2015 at 12:21
  • 1
    You're right that PHP doesn't allow arrays like this, but nor does JSON: your intended output would be invalid JSON, and wouldn't load successfully in Javascript or in any other platform that reads JSON. Commented Aug 7, 2015 at 12:30

5 Answers 5

1

The json you supplied doesn't work like you are asking for, either. It can also only have one value for a given key, so it will end up just like PHP's array. You need to make the values arrays (of strings) instead of strings:

{"11-23-2015":["text","text two"]}
Sign up to request clarification or add additional context in comments.

Comments

1

Had to do with a similar job months ago and I solved as follows. How about getting your data in an associative array like this

$array[$key][] = $value;

UPDATE AFTER YOUR LAST EDIT: you just need this array associative structure and json_encode will do the rest

4 Comments

No! Don't go manually writing JSON! There are a whole stack of problems you'll get from doing that, even if you are diligent with your escaping. That's why json_encode() exists. The fact is, however, that the intended output in the question would not be valid JSON anyway, which is why json_encode() is failing to produce it. But forcing the issue by writing your own encoding function isn't going to help; it's still invalid JSON, so even if you manage to generate it, you won't be able to do much with it at the other end.
OKAY! You're right, I misunderstood the way my calendar works. I'm updating my post. In fact, for each post with the same date, I need to create a row with the same timestamp as key and the multiple titles as value. How is that possible?
Simba you were right, now he's updated the question makes sense and that kind of structure he asked for was not possible, my fault! Now he just need to parse an associative array...
Thanks a lot for the help, I found out how to make it work. :)
0

Why not you try something like this below example, not sure whether it fit with your requirements but i hope :)

$array = array('12', '13', '14');
$array2 = array('test', 'testing1', 'tetst23', 'testing4');
$newarray = array();
foreach ($array as $key => $value) {
    foreach ($array2 as $eacharray) {
        $newarray[$value][] = $eacharray;
    }
}

var_dump(json_encode($newarray));

Output:

{"12":["test","testing1","tetst23","testing4"],"13":["test","testing1","tetst23","testing4"],"14":["test","testing1","tetst23","testing4"]}

Comments

0

I would try somethng like this.

<?php
$date = date('Y-m-d h:i:s');
$date_str = str_replace(array(' ', ':'), '-', $date);
$array[$date_str] = array($value1, $value2, $value3, $valuen);

Comments

-1

The problem you have rests in the fact that the array format you're trying to produce is not valid either in PHP, nor in JavScript or JSON. Indeed, the code you quoted in the question as your desired output would not be valid JSON, and would fail to load in any JSON-compliant system.

My suggestion is to have the text values for each date in their own array. This way you can have more than one per date without breaking PHP's or JSON's array structure. You'd end up with something like this:

var codropsEvents = {
    '11-23-2015' : ['text', 'text two'],
    '11-20-2015' : ['some other text'],
    '11-19-2015' : ['Anything that is text']
};

So structure your PHP data in that format, and use json_encode() to produce the JavaScript.

There are other ways to solve it of course, but I think this should give you the closest match to what you're trying to acheive.

Hope that helps.

2 Comments

Thanks a lot, that made me closer to my goal. Now I'm trying to figure out how to structure that with PHP... Is it possible to append text to a row value if the key is already defined?
Yep, as long as the text is in an array, you should be able to add as many new elements to it as you need to at any point.

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.