0

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!

1
  • var_dump to print whatever type is your variable, echo only for string type Commented Dec 3, 2016 at 23:02

2 Answers 2

1

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

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

Comments

1

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

1 Comment

Thanks it worked but i am sorry if i didn't make your answer because people answered before you

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.