1

I am creating a JSON structure to be passed back to Ajax. I would like to insert 'para' => "Hello" into "content" like this:

{
    "sections": {
        "content": [{
            "para": "Hello"
        }]
    }
}

I tried using this code:

$array = array('sections' => array());
array_push($array["sections"], array("content" => array())); // content must be initialized as empty
array_push($array["sections"][0], array("para" => "Hello"));

But I received this instead:

{
    "sections": [{
        "content": [],
        "0": {
            "para": "Hello"
        }
    }]
}

If I try array_push($array["sections"]["content"], array("para" => "Hello")), I get an error instead. How do I insert an array into "content"? What am I doing wrong?

3
  • tip: create the content array first. then insert it into the general structure to json encode. done that way will allow you to validate with var_dump the array actually contains what you expect. also double-check you're using the right options when calling json_encode. Commented Sep 14, 2014 at 18:26
  • @hakre The problem is that I am using a for loop to process one big chunk of data before I can send anything to json_encode. Thus, I need a direct way to insert objects/arrays into content. It is easy to insert a second dimension of arrays for sections by writing array_push($array['sections'], array('content' => array())) but I have no idea how to use array_push for the next array dimension. Commented Sep 14, 2014 at 20:03
  • array_push(...) is equal to $array[] = $valueToBePushed;. Both do not handle array dimensions because an array first of all is flat, so there is only one dimension. however, nothing stops you to create an array inside an array which is also known as multi-dimensional array. That's why I suggested to build the content array first. This will simplify what you try to achieve. Commented Sep 14, 2014 at 20:28

2 Answers 2

2

If I understood your intentions correctly, here's the array structure you're aiming for:

array("sections" => array(
    "content" => array("para" => "Hello"),
));

However, in Javascript [] represents an array and {} represents an object. If you're trying to create an object with a property of "0", that's not possible in PHP. Variable names have to start with a letter or underscore.

Here's an array of content objects:

$content = new stdClass();
$content->para = 'hello';

array("sections" => array(
    "content" => array($content),
));

To add arrays of contents:

array("sections" => array(
    "content" => array(
        array("para" => "Hello"),
        array("para" => "Hello"),
        array("para" => "Hello"),
    ),
));

You can also construct your own contents array first if you're iterating over an index and then json_encode it. Basic example:

$content = array();

for (i=0; i <3; i++) {
    $content[] = array('para' => 'hello');
}

json_encode(array("sections" => array(
    "content" => array($content),
)));

To convert that to JSON, put your array inside a json_encode() call.

Sign up to request clarification or add additional context in comments.

1 Comment

"content" => array(array("para" => "Hello), array("para" => "World")) is the structure I'm looking for.
1
$array['sections'] = array("content" => array(array("para" => "Hello")));
echo json_encode($array);

will give the result in desired format

2 Comments

Thanks for your input. If I want to do something like this: $array['sections'] = array('content' => array()); and expand from there, what should I do?
You mean how do you keep adding to your content array? Added several more examples.

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.