user745235
user745235

Reputation:

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 '8762' (length=3)

And I need this:

array
  'quadra_id' => string '8762' (length=3)

Upvotes: 0

Views: 283

Answers (6)

pzzd
pzzd

Reputation: 21

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'].

Upvotes: 0

Jeroen
Jeroen

Reputation: 13257

Replace this line...

array_push($where, $quadra_id);

...with the following:

$where ['quadra_id'] = $quadra_id;

Upvotes: 2

mcgrailm
mcgrailm

Reputation: 17638

you just need to supply the index I would do it this way

$where = array();

if($quadra_id != 0) {
   $where['quadra_id']= $quadra_id;
}

Upvotes: 2

Jong Bor Lee
Jong Bor Lee

Reputation: 3855

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;

Upvotes: 4

BenMorel
BenMorel

Reputation: 36574

Replace:

array_push($where, $quadra_id);

With:

$where['quadra_id'] = $quadra_id;

Upvotes: 2

Fosco
Fosco

Reputation: 38526

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);
}

Upvotes: 0

Related Questions