1

i want to convert my array into string i mean i want to echo only array value

i use this code

$query = "SELECT `message` FROM `appstickers`";
$result    = $mysqli->query($query) or die($mysqli->error.__LINE__);
$jsonArray  = array();
 while($row = $result->fetch_assoc()) {
          $jsonArray[]= $row;             
 }     
 //echo $string;
echo json_encode($jsonArray);

i get this output

[{"message":"welcome to team with us"},{"message":"this is for light dispatch"}]

but i want this output

[welcome to team with us,this is for light dispatch"]

so please give me proper solution with example

1
  • you required output makes no sense at all, it's just a concatinated string as the single element of an array Commented Mar 2, 2016 at 10:34

4 Answers 4

3

So why are you using json_encode() ? use implode() instead:

// ... previous code
while($row = $result->fetch_assoc()) {
          $jsonArray[]= $row;             
 }     
echo '[' . implode(',', $jsonArray) . ']';
Sign up to request clarification or add additional context in comments.

1 Comment

i want to send this result to android so i use json_encode
0

I'm going on a limb here, and assuming your required array is not what you actually want, but you do want this:

$jsonArray  = array();
while($row = $result->fetch_assoc()) {
    $jsonArray[]= $row['messsage'];
}
echo json_encode($jsonArray);

which will result in

["welcome to team with us", "this is for light dispatch"]

Comments

0
<?php 
 echo implode(" ",$jsonArray);
?>

Comments

0

Just you need to use implode() function.

$query = "SELECT `message` FROM `appstickers`";
$result    = $mysqli->query($query) or die($mysqli->error.__LINE__);
$jsonArray  = array();
 while($row = $result->fetch_assoc()) {
          $jsonArray[]= $row;             
 }     

//echo $string;
$ans = implode(",",$jsonArray);
echo $ans;

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.