1

I just started learning about JSON. I have to create a static multidimensional array which later required to convert to json encode. Now I get confused in creating multiple array. The below are the code that I have tried. I should have many post and author in this array. But it is printing only single post and author. I am not sure where I exactly did mistake.

<?php
    $data['post']= array(
        'title' => 'This is title',
        'message' =>'This is message',
        'datetime' => 'This is date time',
        'bannerImage' =>''
        );
    $data['author']= array(
        'authorName' => 'Jason Bourne',
        'userType' => 'Registered User',
        'address' => 'New York',
        'profilePic' => 'Profile picture'
        );
    $data['post']= array(
        'title' => 'This is title1',
        'message' =>'This is message1',
        'datetime' => 'This is date time1',
        'bannerImage' =>''
        );
    $data['author']= array(
        'authorName' => 'Jason Bourne1',
        'userType' => 'Registered User1',
        'address' => 'New York1',
        'profilePic' => 'Profile picture1'
        );
    $datas = array($data);
    $rss = (object) array('data'=>$datas);
    $json = json_encode($rss);
    echo $json;
4
  • You will probably notice in the near future that your $data declarations will be overwriting previous declarations because you are reusing the same keys. ...your $data array will only have two subarrays: 'post' (once) and 'author' (once). Commented May 17, 2017 at 2:26
  • @mickmackusa yes how can i solve that Commented May 17, 2017 at 2:28
  • Post, in your question, how you are generating these arrays. They can be simply auto-indexed by php using [], but let's see what you are doing to get here. Commented May 17, 2017 at 2:29
  • You can't have multiple keys with same name, in your above array post field and author field are defined twice. Commented May 17, 2017 at 2:33

1 Answer 1

1

try this, you can check the live demo

<?php

    $data['post']= array(
        'title' => 'This is title',
        'message' =>'This is message',
        'datetime' => 'This is date time',
        'bannerImage' =>''
        );
    $data['author']= array(
        'authorName' => 'Jason Bourne',
        'userType' => 'Registered User',
        'address' => 'New York',
        'profilePic' => 'Profile picture'
        );
    $datas[] = $data;
    $data['post']= array(
        'title' => 'This is title1',
        'message' =>'This is message1',
        'datetime' => 'This is date time1',
        'bannerImage' =>''
        );
    $data['author']= array(
        'authorName' => 'Jason Bourne1',
        'userType' => 'Registered User1',
        'address' => 'New York1',
        'profilePic' => 'Profile picture1'
        );
    $datas[] = $data;
    $json = json_encode(array('data'=>$datas));
    echo $json;
Sign up to request clarification or add additional context in comments.

3 Comments

I am getting the same result
@SS, sorry it's my fault, check it now
thank you very much. that is what i was trying. i tried array_push before but no idea what went wrong. anyways thank you

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.