1

How Can I Trim This Array? I Want to echo First Object of My Array,That is id, I Mean 3 Row of id

 <?php
    $con=mysqli_connect("localhost","root","","arrayy");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
 $sql="SELECT `survey_answers` FROM `user_survey_start`";

if ($result=mysqli_query($con,$sql)){

 while ($row = mysqli_fetch_row($result)){

    $json = $row[0];
    $jason_array  = json_decode($json,true);
    foreach ($jason_array as $data){
        $id[] = $data['id'];
        //$answer[] = $data['answer'];
      // $type[] = $data['type'];

        // here code to insert/update values to db column
    }

    echo implode(',',$id)."</br>";
   // echo implode(',',$answer);
  //  echo implode (',',$type);
  }

}
mysqli_close($con);
?>

And Please Take a Look to Output in This Photo My Output

3
  • print_r($id) and putt in question Commented Feb 16, 2017 at 8:24
  • Incorrect, it give me Array ( [0] => 26 [1] => 30 [2] => 31 [3] => 32 [4] => 33 ) Array ( [0] => 26 [1] => 30 [2] => 31 [3] => 32 [4] => 33 [5] => 40 [6] => 30 [7] => 31 [8] => 32 [9] => 33 ) Array ( [0] => 26 [1] => 30 [2] => 31 [3] => 32 [4] => 33 [5] => 40 [6] => 30 [7] => 31 [8] => 32 [9] => 33 [10] => 100 [11] => 200 [12] => 300 [13] => 400 ) Commented Feb 16, 2017 at 8:26
  • The implode() is wrong way of used. Commented Feb 16, 2017 at 8:35

1 Answer 1

2

You need to re initialize the $id array. Try this

<?php
    $con=mysqli_connect("localhost","root","","arrayy");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
 $sql="SELECT `survey_answers` FROM `user_survey_start`";

if ($result=mysqli_query($con,$sql)){

 while ($row = mysqli_fetch_row($result)){

    $json = $row[0];
    $jason_array  = json_decode($json,true);
    $id = array();
    foreach ($jason_array as $data){
        $id[] = $data['id'];
        //$answer[] = $data['answer'];
      // $type[] = $data['type'];

        // here code to insert/update values to db column
    }

    echo implode(',',$id)."</br>";
   // echo implode(',',$answer);
  //  echo implode (',',$type);
  }

}
mysqli_close($con);
?>
Sign up to request clarification or add additional context in comments.

2 Comments

what is changed here?
Yesssssssss, Appriciate It.

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.