【发布时间】:2015-02-26 18:21:37
【问题描述】:
我需要“重新格式化”来自外部 API 的一些数据,以便它与 Sencha touch 的嵌套列表模块一起使用。我无法更改该外部 API 的数据输出。以下是我从 API 获得的数据示例:
$quest = array(
'gastronomy' => [
'restaurants' => [
'italians' => [
[
'title' => 'Al Castello',
'leaf' => true
],
[
'title' => 'Italia',
'leaf' => true
]
],
'asians' => [
[
'title' => 'Gautam',
'leaf' => true
],
[
'title' => 'Wok',
'leaf' => true
]
]
]
]
);
为了使其与 sencha touch 一起使用,在使用 PHP 服务“重新格式化”后数据必须如下所示:
$result = array(
'items' => [
[
'title' => 'gastronomy',
'items' => [
[
'title' => 'restaurants',
'items' => [
[
'title' => 'italians',
'items' => [
[
'title' => 'Al Castello',
'leaf' => true
],
[
'title' => 'Italia',
'leaf' => true
]
]
],
[
'title' => 'asians',
'items' => [
[
'title' => 'Gautam',
'leaf' => true
],
[
'title' => 'Wok',
'leaf' => true
]
]
]
]
]
]
]
]
);
我已经尝试了所有我能想到的方法,但没有成功。真正困扰我的是所有键都必须重命名为项目。 (因为当我使用递归函数时,我很难访问更深的嵌套项)
【问题讨论】:
标签: php arrays algorithm multidimensional-array sencha-touch