2

i have the following array like this :

{"label":"label1","data":[[10,55],[15,32],[16,49]]}
{"label":"label","data":[[10,55],[15,32],[16,49]]} 

how to get string character (,) beetween {"label":"label1","data":[[10,55],[15,32],[16,49]]} and {"label":"label2","data":[[10,55],[15,32],[16,49]]} result like this..

{"label":"label1","data":[[10,55],[15,32],[16,49]]},
{"label":"label","data":[[10,55],[15,32],[16,49]]} 

Code

while($row = mysql_fetch_assoc($result))
{   
    $int = $row['SC'];
    $join = intval($int);
    $int2 = $row['jam'];
    $join2 = intval($int2);
    $dataset1[] = array($join2,$join);

}
for ($i=0; $i <2 ; $i++) { 
$dataset = array(label => label1, data => $dataset1);
$final = json_encode($dataset);

echo $final;
6
  • Using mysql_fetch_assoc! Then read this. Commented Nov 21, 2014 at 2:08
  • but really, using mysqli_..., because msql_... has been removed from PHP. Commented Nov 21, 2014 at 2:09
  • Sure you don't mean $dataset1[$i] in that for loop? Or are you seriously dumping the same thing over and over? Commented Nov 21, 2014 at 2:11
  • This code only produces one {label....} object. How you would join two or more of them depends on how you generate the others. Commented Nov 21, 2014 at 2:14
  • What is $label = label doing? Should that be $label[] = $row['label']? Commented Nov 21, 2014 at 2:16

1 Answer 1

3

You need to put everything into another array:

$dataset = array();
foreach ($dataset1 as $d) {
    $dataset[] = array('label' => $label, 'data' => $d);
}
$final = json_encode($dataset);
echo $final;

This should output:

[{"label":"label1","data":[[10,55],[15,32],[16,49]]},
 {"label":"label","data":[[10,55],[15,32],[16,49]]}]
Sign up to request clarification or add additional context in comments.

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.