0
    foreach ( $tbl_one_data as $row_ ) {
        $id = ( int ) $row_ [0];
        $time = mysql_real_escape_string ( $row_ [1] );
        $callid = mysql_real_escape_string ( $row_ [2] );
        $queuename = mysql_real_escape_string ( $row_ [3] );

        $arrayValues [] = "($id, '$time','$callid','$queuename',";
    }
}

I am trying to improve the above code with the code below:

$b = 0;
        foreach ( $tbl_one_data as $row_ ) {
            if ($b < count ( $row_ )) {
                ${"var" . $b} = mysql_escape_string ( $row_ [$b] );
                $b ++;
            }
        }

My question is how would I add the dynamic variables created into the array incrementally to achieve something like this: $arrayValues [] = "('$var0','$var1','$var2','$var3'"; ??

$arrayValues [] = "('${"var" . $b}',"; doesn't seem to have the same effect as in my first code snippet.

1
  • Also, move away from mysql_ functions and use mysqli_ or move in to OO PHP with PDO . MySQL_ funtions are deprecated and should no longer be used. Commented May 7, 2015 at 15:17

2 Answers 2

1

I believe this is what you're asking for:

foreach ( $tbl_one_data as $row_ ) {
    $arrayValues [] = "('" . implode("','", $row_) . ",";
}

All the elements in $row_ are concatenated together into a string by implode() using a comma and single quotes as the "glue".

If this doesn't help or isn't what you wanted, leave a comment and we'll try to get better feedback.

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

1 Comment

Surprisingly simple but very effective. Thanks.
0

You can make it simply by using below code:

foreach ( $tbl_one_data as $row_ ) {
    $newArr = array();
    $newArr[] = ( int ) $row_ [0];
    $newArr[] = mysql_real_escape_string ( $row_ [1] );
    $newArr[] = mysql_real_escape_string ( $row_ [2] );
    $newArr[] = mysql_real_escape_string ( $row_ [3] );

    $arrayValues [] = $newArr;
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.