Phpjson array problems

Source: Internet
Author: User
Php json Array problem: $ result1 and $ result2 arrays are merged into one, and echo json_encode ($ result1) is output separately; echo json_encode ($ result2 );
Final output:
{
"Id": "111 ",
"Image_name": "My bedroom",
"Image_url": "d:/image/a.png ",

Comments :[{
Uid, 122 ",
"Cname": "James",
"Ctime ",
}]
}
Echo json_encode (array ($ result1, $ result2) for this effect. if the output is incorrect, it is added directly to the end, instead of a separate array.

What should I do?


Reply to discussion (solution)

What are your $ result1 and $ result2?

What is a separate array? paste the final result to see it.

Echo json_encode (array ("content" => $ result1), $ result2) is that true?

$ Result1 and $ result2 both? Yes? Output.

Echo json_encode (array_merge ($ result1, array ("comments" => $ result2 ??

What are your $ result1 and $ result2?


$ Result 1 is "id": "111 ",
"Image_name": "My bedroom",
"Image_url": "d:/image/a.png ",
These,
$ Result 2

What is a separate array? paste the final result to see it.


It is the "id": "111 ",
"Image_name": "My bedroom",
"Image_url": "d:/image/a.png ",
Followed by $ result2

$ Result1 and $ result2 both? Yes? Output.


$ Result1 indicates the upper half, and $ result2 indicates the lower half.

$ Result1 = array ('id' => 111, 'image _ name' => 'My bedroom ', 'image _ url' => 'd: /image/a.png ',); $ result2 = array ('comments' => array ('uid' => 122, 'cname' => 'xiaoming ', 'ctime' => '2017-12-12 ',); echo json_encode (array_merge ($ result1, $ result2 ));

$ Result1 = array ('id' => '000000', 'image _ name' => 'My bedroom ', 'image _ url' => 'd: /image/a.png '); $ result2 = array ('comments' => array ('uid' => '000000', 'cname' => 'xiaoming ', 'ctime' => '1970-12-12 '); $ result = array_merge ($ result1, $ result2); echo json_encode ($ result );

I think the problem with the landlord seems to be not that simple. do you want to call functions for two arrays?
I did not understand the problem after reading it for a long time.

I think the problem with the landlord seems to be not that simple. do you want to call functions for two arrays?
I did not understand the problem after reading it for a long time.


There are two arrays that output them into a json Array.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.