I need to convert a flat array where the array keys indicate the structure into a nested array where the parent element becomes element zero, i.e. in the example:
$education['x[1]'] = 'Georgia Tech';
It needs to be converted to:
$education[1][0] = 'Georgia Tech';
Here is an example input array:
$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',
);
And here is what the output should be:
$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' ),
),
);
I've banged my head against the wall for hours and still can't get it working. I think I've been looking at it too long. Thanks in advance.
P.S. It should be fully nestable, i.e. it should be able to convert a key that looks like this:
x[1][2][3][4][5][6]
P.P.S. @Joseph Silber had a clever solution but unfortunately using eval()
is not an option for this as it's a WordPress plugin and the WordPress community is trying to stamp out the use of eval()
.