thomasxd24
thomasxd24

Reputation: 55

array_push strings which comes from json_encode for sql insertion

Hello I would like to store string which comes from json encode to get it store in MySQL, but the thing is that the array returned nothing. Here is my code:

    $com = array("1-1","1-2","1-3");
    $classcom=array();
    for ($i=1; $i<(int)$totaleleve; $i++) {
      $eleve=array();
      foreach($com as $value)
       {
        array_push($eleve,'2');

       }
       $final="'".json_encode($eleve)."'";
       array_push($classcom,$final);
       echo $final;
       echo $classcom;
    }
    echo $classcom;

For your information, $totaleleve='20', and I do $final="'".json_encode($eleve)."'"; so that I can concentrate strings of the array to be able to insert it into the SQL statement, which looks something like this:

$sql="INSERT INTO table VALUES (".explode(",", $classcom).")"
// so that it looks like something like this:
$sql="INSERT INTO table VALUES ('["2","2","2"]','["2","2","2"]','["2","2","2"]')

the $final gave me '["2","2","2"]' which i wanted, but when i do array_push($classcom,$final);, it just gave me a blank array :

Array

Can someone help me, please? Thank you!

Upvotes: 0

Views: 89

Answers (2)

SergeyLebedev
SergeyLebedev

Reputation: 3708

Just write print_r($classcom); instead of echo $classcom; and you will see that your array isn't empty.

Upvotes: 1

mitch
mitch

Reputation: 2245

If $totaleleve is greater than 0 the array is not empty. Just use print_r or var_dump to show its values.

print_r($classcom)

Instead of echo $classcom

Upvotes: 1

Related Questions