简体   繁体   English

如何使用php将多维数组转换为正确的JSON?

[英]How can i convert this multidimensional array into correct JSON with php?

I have an array that contains multiple entries of books. 我有一个包含多个书籍条目的数组。 I store each books element with its unique id as its key in each. 我将每个书籍元素及其唯一ID作为其键存储在其中。 I'm struggling on generating correct JSON from this. 我正在努力从中生成正确的JSON。

When i json_encode on the array it just generates each sub arrays json not grouping the elements by key but by which array they're in. 当我在数组上json_encode时,它只会生成每个子数组json,而不是按键将元素分组,而是按它们所在的数组进行分组。

this is my multidimensional array 这是我的多维数组

$booksarray = array("book title" => array(), "book isbn" => array(), "book borrowedcount" => array());

Im completely lost how to get elements out of each sub array and then group them together so that they output something like, 我完全迷失了如何从每个子数组中取出元素,然后将它们分组在一起,以便它们输出类似的内容,

{"results": 
 {"course": "CC120", "books": 
   { "book": 
     [{"id": "12345", "title": "javascript", "isbn": "123456789", "borrowedcount": "45"}] } }

I have the XML outputing as follows 我的XML输出如下

<results>
 <course>cc120</course>
  <books>
   <book id="9876" title="html" isbn="000001234" borrowedcount="56">
   <book id="12345" title="javascript" isbn="123456789" borrowedcount="45">
   <book id="222" title="php5" isbn="55555555" borrowedcount="22">
   <book id="23788" title="XML" isbn="99988877" borrowedcount="5">
  </books>
</results>

Any help will be greatly appreciated 任何帮助将不胜感激

Convert the array into the desired format, then json_encode() it: 将数组转换为所需的格式,然后对其进行json_encode()

$newArray = array();

foreach($booksarray["book title"] as $key => $title)
{
    $newArray[] = array(
        'id' => $key,
        'title' => $title,
        'isbn' => $booksarray["book isbn"][$key],
        'borrowedcount' => $booksarray["book borrowedcount"][$key]
    );
}

echo json_encode($newArray);

It looks like your array isn't in the format you'd like it to be in first. 看起来您的数组的格式不符合您希望的格式。 Once your php array is in the same structure that you want the json to be in, json_encode will work. 一旦您的php数组与您要放入json的结构相同,就可以使用json_encode。

eg 例如

$resultsArray = array(
    "results" => array(
        "course" => "CC120", 
        "books" => array(
            "book" => array(
                "id" => "12345", 
                "title" => "javascript"
                )
            )
        )
    );

$strJson = json_encode($resultsArray);

I think the problem might be in the way that you have structured your array. 我认为问题可能出在您构造数组的方式上。 Are you able to change it? 你能改变吗?

I would suggest an array structure as follows: 我建议如下的数组结构:

$booksarray = array(
    "9876" => array(
         "title" => "html",
         "isbn" => "000001234", 
         "borrowedcount" => "56"
    ),
    "12345" => array(
         "title" => "javascript",
         "isbn" => "123456789", 
         "borrowedcount" => "45"
    )
);

json_encode() should then output this in the format you want. 然后,json_encode()应该以您想要的格式输出。

Eg 例如

{"9876":{"title":"html","isbn":"000001234","borrowedcount":"56"},"12345":{"title":"javascript","isbn":"123456789","borrowedcount":"45"}}

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

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