使用php高效地在json对象中创建一个json对象[关闭](creating a json object inside json object using php efficiently [clos

编程入门 行业动态 更新时间:2024-10-26 22:26:58
使用php高效地在json对象中创建一个json对象[关闭](creating a json object inside json object using php efficiently [closed])

我正在尝试在json对象中创建一个json对象。 我想做的json对象是:

{"post1" : {"id" : "1", "brand" : "brandFromQuery", "model" : "modelFromQuery"}, "post2" : {"id" : "2", "brand" : "brandFromQuery", "model" : "modelFromQuery"}, "post3" : {"id" : "3", "brand" : "brandFromQuery", "model" : "modelFromQuery"}, "post4" : {"id" : "4", "brand" : "brandFromQuery", "model" : "modelFromQuery"}}"

我最终想要做的是创建一个json对象,如上所示,并将其传递到前端。 而数据是SQL查询的结果。 从查询中获取每个“内部”json对象。 关于如何创建这个的任何提示? 会喜欢一些有效的解决方案。 我想在for循环中做循环但是关注效率。

编辑:我正在尝试使用json_encode来实现这一点,但我不知道如何实现这一点。 我到目前为止所尝试的是1) 使用PHP创建JSON对象 (创建一个类并输入数据)。 我真的很喜欢这种方法,因为它创建了一个单独的类,所以它更模块化,但我似乎无法找到一种方法来创建4个不同的帖子。

I'm trying to make a json object inside the json object. The json object I desire to make is:

{"post1" : {"id" : "1", "brand" : "brandFromQuery", "model" : "modelFromQuery"}, "post2" : {"id" : "2", "brand" : "brandFromQuery", "model" : "modelFromQuery"}, "post3" : {"id" : "3", "brand" : "brandFromQuery", "model" : "modelFromQuery"}, "post4" : {"id" : "4", "brand" : "brandFromQuery", "model" : "modelFromQuery"}}"

What I ultimately want to do is create a json object as you see above and pass it along to the front end. And the data is the result of SQL query. Each "inner" json object is fetched from the query. Any tip on how I should create this? Would love some efficient solutions for this. I was thinking of doing for loop in a for loop but concerned about the efficiency.

EDIT: I'm trying to use json_encode to achieve this but i'm not sure how to implement this. what i have tried so far is 1) Create JSON object using PHP (creates a class and inputs the data). I really like this method because it is creating a separate class so its more modular but i just cant seem to find a way to create 4 different posts.

最满意答案

如果每个内部对象都是SQL查询的结果集,那么您只需要单个for循环来创建一种JSON字符串。

创建主数组

$main_array=array();

现在像这样迭代MySQL的结果并将它们附加到主数组中。

/* Your MySQL logic to fetch result */ $count=1; while($row = $result->fetch_assoc()) { // assuming fetch method, you can replace it with yours. $main_array["post".$count]=$row; $count++; } $json_string=json_encode($main_array);

我认为这是您可以创建所需JSON字符串的最佳解决方案。

If each inner object is a result-set from the SQL query then you just need single for-loop to create kind of JSON string.

Create main array

$main_array=array();

Now iterate over result from MySQL like this and append them in main array.

/* Your MySQL logic to fetch result */ $count=1; while($row = $result->fetch_assoc()) { // assuming fetch method, you can replace it with yours. $main_array["post".$count]=$row; $count++; } $json_string=json_encode($main_array);

I think this is the best solution in which you can create the required JSON string.

更多推荐

本文发布于:2023-07-04 17:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1027066.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   高效   创建一个   json   php

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!