1

I have this loop:

foreach ($tables as $table => $queries) {
                    foreach ($queries as $query) {
                        $insert = array($query['column'] => $query['value']);
                    }

The $insert associative array should be incremented of new elements each loop, so that the logical result woud be:

 $insert = array($query['column'] => $query['value'], $query['column'] => $query['value'] ...etc);

I tried using $insert[] and $insert .= and $insert += but none of these give the expected result

Thanks for any help

1
  • you should also declare your $insert = array(); on the top. so we don't look like bunch of lazy php developers who don't declare our variables before we use them. Commented Jul 11, 2013 at 15:17

4 Answers 4

5

You are looking for this

$insert[] = 
Sign up to request clarification or add additional context in comments.

4 Comments

@DevZer0 he should continue with trying $insert[], but add the equals to it, $insert[] =
ok so we think he didn't put the = sign for assignment, anyway +1
This is returning child arrays and as stated in my question this is not what I am looking for. Thanks anyway
@DevZer0 ;) anyway half is deserved anyway!
2

To insert in array use:

$insert[] = array($query['column'] => $query['value']);

Comments

1

Once the array's been defined, you have to use

$insert[$query['column']] = $query['value']; // sample #1

to specify a new key/value pair within the $insert array.

If you use

$insert[] = array(...); // sample #2

you'll just be inserting a new child array that contains a single key/value pair.

e.g. $insert before

$insert = array(
   'foo' => 'bar'
);

$insert after sample #1

$insert = array(
    'foo' => 'bar',
    'baz' => 'qux'
);

$insert after sample #2:

$insert = array(
   'foo' => 'bar'
   0 => array(
        'baz' => 'qux'
   )
);

3 Comments

insert after sample 2 is messed up don't you think?
you have baz inside another array. i am confused where u getting that structure from
baz/qux are the two bits of $query, basically.
0
$insert = array();

foreach ($tables as $table => $queries) 
{

    foreach ($queries as $query) 
    {
            $insert[$query['column']] = $query['value'];      
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.