1

I have to write json result in a string.

Here is my code,

<!DOCTYPE html> <html lang="en">
     <head>
         <meta charset="UTF-8">
         <title></title>
     </head>
     <body>
         <form method="POST">
             Enter Pin <input type="text" name="pinCode">
             <input type="submit" name="formSubmit">
         </form>
     </body> </html>


 <?php

     if(isset($_POST['formSubmit']))
     {
         $input = $_POST['pinCode'];
         $shortUrl=file_get_contents("https://www.whizapi.com/api/v2/util/uiin/indian-city-by-postal-code?project-app-key=<app_key>&pin=".$input);
         $res = json_decode($shortUrl, true);

         echo implode($res);
     }

 ?>

Currently the outcome is on json format. I have to print the result in string. e.g - { "title" : "Mr", "name" : "sandeep"}. Result would be like "Mr sandeep". That's why I have used json_decode for changing json into array but then I couldn't understand how to change associative array in string.

Thanks in advance.

2
  • json result in a string? what you actually wan to achieve. put some input data and expected outcome based on tat input to make yourself clear to us Commented Aug 14, 2016 at 9:34
  • Currently the outcome is on json format. I have to print the result in string. e.g - { "title" : "Mr", "name" : "sandeep"}. Result would be like "Mr sandeep" Commented Aug 14, 2016 at 10:51

1 Answer 1

1

Don't decode the json if you want it in string. The output is string and you are converting it to array using json_decode so just comment that line

if(isset($_POST['formSubmit']))
{
    $input = $_POST['pinCode'];
    $shortUrl=file_get_contents("https://www.whizapi.com/api/v2/util/ui/in/indian-city-by-postal-code?project-app-key=<app_key>&pin=".$input);
    //$res = json_decode($shortUrl, true);

    echo $shortUrl;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply, but currently the outcome is on json format. I have to print the result in string. e.g - { "title" : "Mr", "name" : "sandeep"}. Result would be like "Mr sandeep". That's why I had used json_endcode for changing json in array and then I coudn't understand that how to print associative array in string.
Can provide the json format and the desired output from that

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.