PHP - 使用数组进行动态创建时遇到困难
我需要一个像这样的数组:
array('quadra_id'=>$quadra_id);
问题是我将根据表单发送的内容动态创建它。
$where = array();
if($quadra_id != 0) {
array_push($where, $quadra_id);
}
它返回给我这个:
array
0 => string '8762' (length=3)
我需要这个:
array
'quadra_id' => string '8762' (length=3)
I need a array like this one:
array('quadra_id'=>$quadra_id);
The deal is that I'll create it dynamically, according to what is sent by the form.
$where = array();
if($quadra_id != 0) {
array_push($where, $quadra_id);
}
It returns me this:
array
0 => string '8762' (length=3)
And I need this:
array
'quadra_id' => string '8762' (length=3)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
array_push 将新元素添加到带有数字索引的数组中,而您想要的是字符串索引。所以你实际上想要这样做:
array_push adds the new element to the array with a numeric index, while what you want is a string index. So you actually want to do this:
替换:
为:
Replace:
With:
你只需要提供索引我会这样做
you just need to supply the index I would do it this way
将此行...
array_push($where, $quadra_id);
...替换为以下内容:
$where ['quadra_id'] = $quadra_id;
Replace this line...
array_push($where, $quadra_id);
...with the following:
$where ['quadra_id'] = $quadra_id;
你正在寻找的是:
如果只有一个,你真的应该这样做:
What you're looking for is:
If there's only going to be one, you should really just do:
您可以将 $where 设置为等于 $_POST。如果您有其他不应该出现在 $where 中的表单输入,您可以通过将它们放入输入名称的数组中来将它们放在一边,如下所示。
在这种情况下,$where 将设置为 $_POST['where']。
You can set $where equal to $_POST. If you have other form inputs that should not be in $where, you can set them aside by putting them in an array in the input name, like this.
In this case, $where would be set to $_POST['where'].