php 打印嵌套数组,将平面PHP数组转换为基于数组键的嵌套数组?

博客内容涉及将平面数组转换为嵌套数组结构的问题,其中数组键包含层级信息,如'x[1][2][3][4][5][6]'。作者提到需要避免使用eval()函数,因为是在WordPress插件中,而WordPress社区正试图减少对该函数的依赖。目标是创建一个完全可嵌套的数组结构,与输入数组相对应。
摘要由CSDN通过智能技术生成

我需要转换一个平面数组,其中数组键将结构指示为一个嵌套数组,其中父元素变为零,即在示例中:

$education['x[1]'] = 'Georgia Tech';

它需要转换为:

$education[1][0] = 'Georgia Tech';

这是一个输入数组的例子:

$education = array(

'x[1]' => 'Georgia Tech','x[1][1]' => 'Mechanical Engineering','x[1][2]' => 'Computer Science','x[2]' => 'Agnes Scott','x[2][1]' => 'ReligIoUs History','x[2][2]' => 'Women\'s Studies','x[3]' => 'Georgia State','x[3][1]' => 'Business Administration',);

这里是什么输出应该是:

$education => array(

1 => array(

0 => 'Georgia Tech',1 => array( 0 => 'Mechanical Engineering' ),2 => array( 0 => 'Computer Science' ),),2 => array(

0 => 'Agnes Scott',1 => array( 0 => 'ReligIoUs History' ),2 => array( 0 => 'Women\'s Studies' ),3 => array(

0 => 'Georgia State',1 => array( 0 => 'Business Administration' ),);

我的头撞在墙上几个小时,仍然无法使其工作.我想我一直在看它太久了.提前致谢.

附:它应该是完全可嵌套的,即它应该能够转换一个如下所示的键:

x[1][2][3][4][5][6]

P.P.S. @Joseph Silber有一个聪明的解决方案,但不幸的是,使用eval()不是一个选项,因为它是一个wordpress插件,wordpress社区正在试图消除eval()的使用.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值