简体   繁体   English

使用相同的键为多个值创建一个PHP数组

[英]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. 我正在尝试改编一个称为“弹性日历”的简单jquery代码,并使用元密钥通过我们的管理员填充事件。

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: 为了使用jQuery日历,我可以像这样动态创建JSON对象:

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 : 但是我需要获取一个JSON对象,如下所示:

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. 关键是,PHP不允许在多行中使用相同的键:如果我有多个具有相同日期(wp元值)的帖子,则PHP仅在数组中存储一个。

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? 如何组织我的PHP数组以获得JSON对象的良好格式?

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. 对于每个具有相同日期的帖子,我需要创建一个具有唯一键(日期)和唯一值的行,该值将是包含所有帖子标题的字符串。

The Javascript object is not a valid object. Javascript对象不是有效的对象。

for (var properties in your_obj) {
   // will not return dups
}

If they are different entries and you want to treat them as such, AND you have access to the JSON creation, make it like this: 如果它们是不同的条目,并且您想这样对待它们,并且您有权访问JSON创建,则使其如下所示:

[
  {
    "date" : "11-23-2015",
    "content" : "text here"
  },
  {
    "date" : "11-23-2015",
    "content" : "text 2 here"
  },
  {
    "date" : "11-23-2015",
    "content" : "text 3 here"
  }
]

then in you php use: 然后在你的PHP使用:

json_decode($jsonstring);
json_decode($string, true); // for nested array

see http://php.net/manual/en/function.json-decode.php 参见http://php.net/manual/en/function.json-decode.php

there was a very similar question: https://stackoverflow.com/a/12561521/4792497 有一个非常相似的问题: https : //stackoverflow.com/a/12561521/4792497

You can do like this; 你可以这样

JSON JSON格式

var codropsEvents = {
"11-23-2015":[
   "text",
   "some text",
   "more text",
   "bla bla"]
}

PHP 的PHP

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM