PHP:从平面标量值填充嵌套数组
问题
我有一个嵌套的 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 数组语法的问题。
PROBLEM
I have a nested PHP array that I need to populate from flat scalar
values. The problem is I cannot know ahead of time what the structure
of the nested PHP array will be until I get the request to fill in
the flat scalar values.
EXAMPLE
// 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?
QUESTION
I know I must not be the first person who has needed to convert from flat name-value pairs into a nested PHP array (or nested array in any programming language). What is the established way (if any) of converting these flat scalar name-value pairs into the appropriate PHP nested array?
Just to re-iterate, I cannot know ahead of time what the name-value pairs will be for populating the array, that is one constraint I am dealing with here.
UPDATE
The fact that I cannot know the values (or, if you prefer, the Array keys that get populated by the scalar-value-representations) is a constraint of the particular problem space I am dealing with. This is not a question about basic PHP array syntax.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
注意:下面的 PHP 代码是一个 hack,而是这样做。有人之前发布了更好的解决方案,但该帖子现在已经消失了。简而言之,您已经可以将表单中的值数组提交给 PHP:
name 属性中的方括号完全按照您的想法进行操作。提交时,
$_POST['contact_info']
将是一个包含三个键的数组:fname
、lname
和middle.
如果可能的话,您应该使用此方法而不是我在下面编写的代码。它更干净、更好、更易于维护,这就是它应该做的方式。
这是一个有趣的挑战。我们将使用 PHP 有趣的方式来进行引用,以发挥我们的优势。假设:
那么这个函数可能是您的起点。
再次强调,这是一个 hack。您应该使用 PHP 表单处理来解决这个问题。
Note: My PHP code below is a hack, instead do this. Someone had posted a better solution earlier, but the post is gone now. In short, you can already submit arrays of values in forms to PHP:
The square brackets in the name attribute do exactly what you think they might do. On submit,
$_POST['contact_info']
will be an array with three keys,fname
,lname
, andmiddle
.If at all possible, you should use this method rather than the code I've written below. It's cleaner, it's better, it's more maintainable, it's the way it should be done.
This is a fun challenge. We're going to use PHP's funny way of doing references to our advantage. Given that:
Then this function might be a starting point for you.
Again, this is a hack. You should use PHP form handling to take care of this for you.
我不确定你说你无法提前知道名称-值对是什么意思。
参考您的示例,这将起作用:
提前不知道值是给定的 - 这就是用户输入的方式。
您必须提前知道密钥。如果没有此信息,您将无法知道如何将
$_GET
中的值映射到$person
中的值。如果您事先不知道密钥,则无法解决此问题。如果您事先不知道密钥,则您的软件设计存在严重缺陷。
I'm not certain what you mean by saying that you cannot know ahead of time what the name-value pairs will be.
In reference to your example, this will work:
Not knowing the values in advance is a given - that's the way it is with user input.
You must know the keys in advance. Without this information you cannot know how to map values in
$_GET
to values in$person
.If you don't know the keys in advance you cannot solve this problem. If you don't know the keys in advance there is a serious flaw in the design of your software.