在 PHP 中使用 JSON/RPC API 的多维数组
我正在使用 RPC 风格的一些基于 JSON 的 API,在通过一些 PHP 包装器与 API 交互后,我得到了这个多维数组:
Array
(
[ok] => 1
[taskgroup] => Array
(
[task_1] => Array
(
[id] => task_1
[name] => Task 1
[url] => /tasks/1/
[description] => Lorem ipsum dolor sit amet...
[img] => http://example.net/mypic1442421.png
[related_tasks] => Array
(
[0] => task_18
)
)
[task_2] => Array
(
[id] => task_2
[name] => Task 2
[url] => /tasks/2/
[description] => Lorem ipsum dolor sit amet...
[img] => http://example.net/mypic1442421.png
[related_tasks] => Array
(
[0] => task_9
[1] => task_23
)
)
)
)
我的问题是,如何遍历这个多维数组?对于这种md数组响应有一些相关的方法吗?更具体地说 - 举一些可以阐明这个问题的例子 - 我如何打印所有图像和 url 的列表,如 taskArray[][task_n][img] - taskArray[][task_n][url ]
? --task_n 可以是任何非连续的字母数字值
我尝试了一些我在这里找到的递归方法,但没有运气,如果我再次问同样的事情,请原谅。请原谅这个n00bness。
I'm working with some JSON based APIs in RPC style and I'm getting this multidimensional arrays after interacting with the APIs via some PHP wrappers:
Array
(
[ok] => 1
[taskgroup] => Array
(
[task_1] => Array
(
[id] => task_1
[name] => Task 1
[url] => /tasks/1/
[description] => Lorem ipsum dolor sit amet...
[img] => http://example.net/mypic1442421.png
[related_tasks] => Array
(
[0] => task_18
)
)
[task_2] => Array
(
[id] => task_2
[name] => Task 2
[url] => /tasks/2/
[description] => Lorem ipsum dolor sit amet...
[img] => http://example.net/mypic1442421.png
[related_tasks] => Array
(
[0] => task_9
[1] => task_23
)
)
)
)
My question is, how can I go through this multi-dimensional arrays? Is there some related methods for this kind of md-array responses? More concretely -to take on some example that could bring light to this thing- how can I print a list of all the images and url like taskArray[][task_n][img] - taskArray[][task_n][url]
? --task_n can be any non-sequential alphanumeric value
I tried some recursive methods I found here but without luck, please excuse if I'm asking again the same thing. And excuse the n00bness.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常,该数组将使用如下代码进行处理:
Typically, that array would be processed with code something like this: