Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
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. –  hakre yesterday
    
@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. –  Dennis yesterday
    
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. –  hakre yesterday

2 Answers 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.

share|improve this answer
    
"content" => array(array("para" => "Hello), array("para" => "World")) is the structure I'm looking for. –  Dennis yesterday
$array['sections'] = array("content" => array(array("para" => "Hello")));
echo json_encode($array);

will give the result in desired format

share|improve this answer
    
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? –  Dennis yesterday
    
You mean how do you keep adding to your content array? Added several more examples. –  Haig Bedrosian yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.