0

I have this array:

Array
(
    [users] => Array
        (
            [0] => Array
                (
                    [column] => email
                    [value] => [email protected]
                )

            [1] => Array
                (
                    [column] => nickname
                    [value] => dpucci
                )

        )

    [social] => Array
        (
            [0] => Array
                (
                    [column] => user_id
                    [value] => opweirpor
                )

        )

)

Starting from this array I'll have to build a string that will look like this:

insert(users, array(email=>[email protected],nickname=>dpucci)

And another like this:

insert(social, array(user_id=>opweirpor)

This is what I am doing:

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

The problem is that the result I am getting is the following:

insert(users, array(email=>[email protected],nickname=>dpucci)

and:

insert(social, array(email=>[email protected], nickname=>dpucci, user_id=>opweirpor)

This is because the variable $insert is incremented each new loop and it is adding ALL the results instead that only the ones I need for each $tables loop.

How can I achieve the expected result?

2 Answers 2

3

Because you keep appending your string to the same $insert in the inner loop, which is ran many times. Just clear the insert variable after you have finished with it, i.e. after your inner loop. (Untested)

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

    // add this line
    $insert = "";

}

or, put it before the inner foreach loop, which has an advantage to ensure $insert is not polluted with previous codes or otherwise uninitialized giving PHP warnings.

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

    // add this line
    $insert = "";

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

    echo 'insert(' . $table . ', array(' . $insert . ')';

}

However, your code actually creates

insert(users, array(email=>[email protected],nickname=>dpucci,)

Note the comma after dupcci. Which I don't think is what you want. To fix this, simply remove the trailing comma with substr:

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

    // add this line
    $insert = "";

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

    // add one more line here
    $insert = substr($insert, 0, -1);

    echo 'insert(' . $table . ', array(' . $insert . ')';

}

Also, check your desired output. It seems that the brackets are not balanced and the strings are unquoted. Are you sure it is what you want?

Sign up to request clarification or add additional context in comments.

Comments

2

Reset the variable $insert.

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

Comments

Your Answer

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