1

this php script

 <?php
 require('medoo.min.php');
 header('Content-type: application/json');
 $database = new medoo('brickx');

 $datas = $database->select("customer", ["Companyname", "Phonenumber"]);

 $jsonresponse = array("customers"=> array());
 array_push($jsonresponse["customers"], $datas);
 echo (json_encode($jsonresponse));

 ?>

outputs this kind of json:

 {"customers":[[{"Companyname":"company1","Phonenumber":"+567890789"},{"Companyname":"company2","Phonenumber":"5678905678"}]]}

however i need to get rid of the square brackets and get something like

{"customers":{"Companyname":"company1","Phonenumber":"+567890789"},{"Companyname":"company2","Phonenumber":"5678905678"}}

any suggestions?

2
  • 6
    What you want is not legal JSON. You have to keep at least one square bracket. Commented Sep 24, 2013 at 17:27
  • in some of the documentation of restkit the examples had json without square brackets. you are right if you say that i need at least one square bracket. the answer below is the correct one. thanks all for helping Commented Sep 24, 2013 at 17:32

1 Answer 1

1

$datas is already an array, you probably want this instead:

$array = array('customers' => $datas);
echo json_encode($array);
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.