我创建了以下代码来循环json数据,并将密钥放在一个数组中,以便稍后引用它们。我做了我需要的但它没有经过优化,应该有一个更好的方法来实现这个功能。 json数据改变了不同类型的时间。在示例中,我使用来自分类源的json数据。
我尝试了两种直接的方法来实现这个功能,我可以重复使用,而不是一直重新输入这些代码。 - 如果值是数组则调用自身的函数 - 传递参考fuinction 但我没能让它发挥作用。
我的问题是,如果有人就此提出建议,我应该学习哪些主题来理解这一点。
{
"categories": {
"0": {
"@attributes": {
"category_id": "2"
},
"category_id": "2",
"parent_id": "1",
"categories_name": "Dutch name",
"isActive": 1,
"categories_image": "",
"categories_image_alt": "",
"sort_order": "1",
"date_added": "01-07-2014",
"last_modified": "30-01-2017",
"categories_name_languages": {
"dutch": {
"name": "Dutch name"
}
},
"description": {
"dutch": {
"above": {
"@cdata": ""
},
"below": {
"@cdata": ""
}
}
},
"meta": {
"dutch": {
"url": "dutch-url",
"title": "meta dutch title string"
}
}
}
}
}
PHP:
$mapping = array();
foreach ($jsonResult['categories'][0] as $key => $value) {
if(is_array($value)) {
foreach ($value as $key2 => $value2) {
if(is_array($value2)) {
foreach ($value2 as $key3 => $value3) {
if(is_array($value3)) {
foreach ($value3 as $key4 => $value4) {
if(is_array($value4)) {
$mapping[] = $value4;
}else {
$mapping[] = '[\''. $key . '\'][\'' . $key2 . '\'][\'' . $key3 . '\'][\'' . $key4 . '\']';
}
}
}else {
$mapping[] = '[\''. $key . '\'][\'' . $key2 . '\'][\'' . $key3 . '\']';
}
}
}else {
$mapping[] = '[\''. $key . '\'][\'' . $key2 . '\']';
}
}
} else {
$mapping[] = '[\''. $key . '\']';
}
}
结果是:
(
[0] => ['@attributes']['category_id']
[1] => ['category_id']
[2] => ['parent_id']
[3] => ['categories_name']
[4] => ['isActive']
[5] => ['categories_image']
[6] => ['categories_image_alt']
[6] => ['sort_order']
[8] => ['date_added']
[9] => ['last_modified']
[10] => ['categories_name_languages']['dutch']['name']
[11] => ['description']['dutch']['above']['@cdata']
[12] => ['description']['dutch']['below']['@cdata']
[13] => ['meta']['dutch']['url']
[14] => ['meta']['dutch']['title']
)
然后每次如果我使用基于json的类别,我可以加载此映射并确切知道从多个阵列获取值的位置。
添加了代码@Barmar
function mapArray($arr) {
$output = [];
$map = [];
foreach ($arr as $key => $value) {
if (is_array($value)) {
foreach(mapArray($value) as $flattenKey => $flattenValue) {
$output["${key}][${flattenKey}"] = "['${key}']['${flattenKey}']";
$map[] = "['${key}']['${flattenKey}']";
}
} else {
$output[$key] = $key;
$map[] = $key;
}
}
return $output;
}
新输出:
Array
(
[@attributes][category_id] => ['@attributes']['category_id']
[category_id] => category_id
[parent_id] => parent_id
[categories_name] => categories_name
[isActive] => isActive
[categories_image] => categories_image
[categories_image_alt] => categories_image_alt
[sort_order] => sort_order
[date_added] => date_added
[last_modified] => last_modified
[categories_name_languages][dutch][name] => ['categories_name_languages']['dutch][name']
[description][dutch][aboveProducts][@cdata] => ['description']['dutch][aboveProducts][@cdata']
[description][dutch][belowProducts][@cdata] => ['description']['dutch][belowProducts][@cdata']
[meta][dutch][categories_seo_url] => ['meta']['dutch][categories_seo_url']
[meta][dutch][categories_meta_title] => ['meta']['dutch][categories_meta_title']
)
输出$ map:
Array
(
[0] => ['@attributes']['0']
[1] => category_id
[2] => parent_id
[3] => categories_name
[4] => isActive
[5] => categories_image
[6] => categories_image_alt
[7] => sort_order
[8] => date_added
[9] => last_modified
[10] => ['categories_name_languages']['0']
[11] => ['description']['0']
[12] => ['description']['1']
[13] => ['meta']['0']
[14] => ['meta']['1']
)