0

I have an array that contains multiple entries of books. I store each books element with its unique id as its key in each. I'm struggling on generating correct JSON from this.

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.

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

<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

3
  • Does json_encode() not work? php.net/manual/en/function.json-encode.php Commented Feb 26, 2013 at 17:03
  • 3
    The easiest way would be to get the php array into the correct format, then do the json_encode. Commented Feb 26, 2013 at 17:03
  • Can you show us the sub arrays? Commented Feb 26, 2013 at 17:03

3 Answers 3

2

Convert the array into the desired format, then json_encode() it:

$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);
Sign up to request clarification or add additional context in comments.

3 Comments

Better to use ISBN as the "authoritative" source for looping i think.
Thank you this worked perfectly. My knowledge of arrays definitely needs work but thank you very much for the answers!
@prodigitalson It won't make a difference unless there are books that don't have a title but have an ISBN. In any case, the better solution is to parse it into the desired format originally, instead of parsing into the three arrays and then re-formatting it.
1

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.

e.g.

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

$strJson = json_encode($resultsArray);

Comments

0

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.

E.g.

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

1 Comment

I misread the output format you wanted this in, jchapa's response is probably the cleanest way to achieve this.

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.