1

I have a php function that currently looks like this

function renderJson(PageArray $items) {
$myData = array();
$myData['title'] = [];
$myData['url'] = [];

// cycle through all the items
foreach($items as $item) {

    array_push($myData["title"],$item->title);
    array_push($myData["url"],$item->url);


}

return json_encode($myData);
}

It currently works in this shape as output

{"title":["A","B"],"url":["A","B"]}

But this is what I am trying to achieve

{"title":["A"],"url":["A"]},{"title":["B"],"url":["B"]}

I am trying to return it as a JSON encode.The $item->title and $item->url returns a string.Any help would be great.

1
  • You would need to construct an array of objects instead of an object with arrays inside it. What have you tryed? Commented Apr 3, 2018 at 14:05

3 Answers 3

4

So now you get this :

{"title":["A","B"],"url":["A","B"]}

But you want this :

{"title":["A"],"url":["A"]},{"title":["B"],"url":["B"]}

What you want is add a new "item" each time you loop and each item have a title and an url. Right now you return an array that contain 2 sub-array : one for the title, one for the url.

Try this way :

function renderJson(PageArray $items) {
    // Your result array
    $myData = array();

    // cycle through all the items
    foreach($items as $item) {

        // Each time you loop, you add a new array in your main array with a title and an URL
        $myData[] = array(
            "title" => $item->title,
            "url"   => $item->url
        );
    }

    return json_encode($myData);
}

I used [] because he does what you need : push element at the end of your main array. It's the short way to use array_push(). Here is the documentation for more details : http://php.net/manual/en/function.array-push.php

The output will be :

[{"title":["A"],"url":["A"]},{"title":["B"],"url":["B"]}]
Sign up to request clarification or add additional context in comments.

6 Comments

Please dont just post code as an answer, try and explain what you did and why this works :)
This helps alot though, please update and explain it more
@MickaelLeger Why is there a [ ] in $myData, is it for adding inside the subarray?
@Squish I had a link for the documentation. It's the short way to use array_push : here I add a subarray in your main array for each item you have in items
@MickaelLeger Thank you for the help, I learned something new today
|
1

What you want to return is not, of course, valid json. At this point return an array containing those two objects (i.e., push the whole $item to $myData)

function renderJson(PageArray $items) {
    $myData = array();

    // cycle through all the items
    foreach($items as $item) {
        array_push($myData,['title' => $item->title, 'url' => $item->url]);
    }

    return json_encode($myData);
}

which gives you something in the form of

[{"title":"A","url":"A"},{"title":"B","url":"B"}]

Comments

0

You create invalid JSON by concatenation your JSON by comma. Either get your client (speak receiver of the json data) to split the result by comma and then parsing each JSON by its own.

Otherwise you could create a "big" JSON string, containing both (or all if there are more) of your JSON data.

Just create an array in PHP, add a new element for each JSON data, then json_encode() it and go over it by a loop in your client.

What you will use in the end, doesnt really matter that much, but I would use the second approach by a lot over the first, since it´s much cleaner.

Comments

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.