问题
我有一个嵌套的 PHP 数组,我需要从平面标量值中填充它。问题是在收到填充平面标量值的请求之前,我无法提前知道嵌套 PHP 数组的结构是什么。
例子
// example where we populate the array using standard PHP
$person['contact_info']['fname'] = 'Attilla';
$person['contact_info']['lname'] = 'Hun';
$person['contact_info']['middle'] = 'The';
$person['hobbies'][0] = 'Looting';
$person['hobbies'][1] = 'Pillaging';
// example where we populate the array from flat scalar values
// (these are obtained from the user via name-value pairs)
// how can I correctly populate $person from this??
print($_GET['contact_info.fname']); // 'Smokey';
print($_GET['contact_info.middle']); // 'The';
print($_GET['contact_info.lname']); // 'Bear';
// how can I correctly populate $person from this??
print($_GET['contact_info.fname']); // 'Jabba';
print($_GET['contact_info.middle']); // 'The';
print($_GET['contact_info.lname']); // 'Hutt';
// How can I use these three flat scalars
// to populate the correct slots in the nested array?
问题
我知道我一定不是第一个需要将平面名称-值对转换为嵌套 PHP 数组(或任何编程语言中的嵌套数组)的人。将这些平面标量名称-值对转换为适当的 PHP 嵌套数组的既定方法(如果有)是什么?
只是为了重新迭代,我无法提前知道用于填充数组的名称-值对是什么,这是我在这里处理的一个约束。
更新
我不知道值(或者,如果您愿意,由标量值表示填充的数组键)这一事实是我正在处理的特定问题空间的约束。这不是关于基本 PHP 数组语法的问题。