通过PHP重复密钥的JSON数据

Apache SOLR calls for a quirky JSON format when you want to add multiple entries at once. ( http://wiki.apache.org/solr/UpdateJSON#Update_Commands -- notice the 'add' nodes)

In a nutshell, I need to be able to create JSON that looks like this:

{
   "key": "val 1",
   "key": "val 2"
}

In PHP, you can easily create an array, but this JSON structure calls for an object with two keys of identical name that are explicitly not in an array.

You aren't going to be able to do this using json_encode because it's not valid JSON. (Keyspace collision)

You are going to need to assemble the object manually.

You might consider creating the individual items, then using implode(). Then you can prepend and append { and }.

This would actually feel cleaner to me then hacking away at PHP to get json encoding to work.

Suggested Alternative

I'm a big fan of the Solarium Project for PHP and Solr.

Bulk Adds and pretty easy take a look here: http://wiki.solarium-project.org/index.php/V2:BufferedAdd_plugin