首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >php数组继续向纵深推进

php数组继续向纵深推进
EN

Stack Overflow用户
提问于 2017-11-11 00:20:52
回答 1查看 66关注 0票数 0

我当前的代码如下:

代码语言:javascript
复制
<?php
$item = "123.456.789.963.852.741";
$item_arr = explode(".", $item);
$inner_count = count($item_arr);
$parent_element = "myarray";

if($inner_count==3){
    $my_array[$item_arr[0]]['XYZ-Key'][$item_arr[1]]['XYZ-Key'][$item_arr[2]]['order'] = $count;
    $my_array[$parent_element]['XYZ-Key'][$item_arr[1]]['XYZ-Key'][$item_arr[2]]=$my_array[$item_arr[0]]['XYZ-Key'][$item_arr[1]]['XYZ-Key'][$item_arr[2]];
    unset($my_array[$item_arr[0]]['XYZ-Key'][$item_arr[1]]['XYZ-Key'][$item_arr[2]]);
}

if($inner_count==4){
    $my_array[$item_arr[0]]['XYZ-Key'][$item_arr[1]]['XYZ-Key'][$item_arr[2]]['XYZ-Key'][$item_arr[3]]['order'] = $count;
    $my_array[$parent_element]['XYZ-Key'][$item_arr[1]]['XYZ-Key'][$item_arr[2]]['XYZ-Key'][$item_arr[3]]=$my_array[$item_arr[0]]['XYZ-Key'][$item_arr[1]]['XYZ-Key'][$item_arr[2]]['XYZ-Key'][$item_arr[3]];
    unset($my_array[$item_arr[0]]['XYZ-Key'][$item_arr[1]]['XYZ-Key'][$item_arr[2]]['XYZ-Key'][$item_arr[3]]);
}

if($inner_count==5){
    $my_array[$item_arr[0]]['XYZ-Key'][$item_arr[1]]['XYZ-Key'][$item_arr[2]]['XYZ-Key'][$item_arr[3]]['XYZ-Key'][$item_arr[4]]['order'] = $count;
    $my_array[$parent_element]['XYZ-Key'][$item_arr[1]]['XYZ-Key'][$item_arr[2]]['XYZ-Key'][$item_arr[3]]['XYZ-Key'][$item_arr[4]]=$my_array[$item_arr[0]]['XYZ-Key'][$item_arr[1]]['XYZ-Key'][$item_arr[2]]['XYZ-Key'][$item_arr[3]]['XYZ-Key'][$item_arr[4]];
    unset($my_array[$item_arr[0]]['XYZ-Key'][$item_arr[1]]['XYZ-Key'][$item_arr[2]]['XYZ-Key'][$item_arr[3]]['XYZ-Key'][$item_arr[4]]);
}

现在我想把它扩展到更多的计数(现在的代码是5)

但问题是我不能用同样的方式写代码

EN

回答 1

Stack Overflow用户

发布于 2017-11-11 00:36:50

你可以这样做。只需为您的格式更改字符串

代码语言:javascript
复制
$item = "1.2.3.4.5";
$ar = explode('.', $item);
$start = '{';
$end = '}';
foreach($ar as $i) {
    $start .= '"'.$i.'":{';
    $end = '}'.$end;
}

$temp =  $start . '"order":{}' .$end; // {"1":{"2":{"3":...{"order":{}}}}}

$res = json_decode($temp,true);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47227089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档