PHP - Difficulty working with array for dynamic creation
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 '开发者_如何学Python8762' (length=3)
And I need this:
array
'quadra_id' => string '8762' (length=3)
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:
$where['quadra_id'] = $quadra_id;
Replace:
array_push($where, $quadra_id);
With:
$where['quadra_id'] = $quadra_id;
you just need to supply the index I would do it this way
$where = array();
if($quadra_id != 0) {
$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:
array_push($where, array('quadra_id'=>$quadra_id));
If there's only going to be one, you should really just do:
if($quadra_id != 0) {
$where = array('quadra_id'=>$quadra_id);
}
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.
<input name='where[quadra_id]' value='' />
In this case, $where would be set to $_POST['where'].
精彩评论