Search code examples
phparraysarray-push

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)

Solution

  • 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;