0

This is how I'm getting result while I'm trying to encode string in JSON format. I want that url should be in this: http://ipaddress/Up/user_images/549279.jpg

RESULT:

{"result":[{"id":"42","name":"http://IPADDRESS/Up/user_images/380289.jpg"},{"id":"43","name":"http://IPADDRESS/Up/user_images/995062.jpg"},{"id":"44","name":"http://IPADDRESS/Up/user_images/171544.jpg"},{"id":"41","name":"http://IPADDRESS/Up/user_images/549279.jpg"}]}

Warning: json_decode() expects parameter 1 to be string, array given in C:\xampp\htdocs\jsn\PHP_Scripts\getAllEmp.php on line 26

//creating a blank array 
$result = array();
$r1 = array();

//looping through all the records fetched
while ($row = mysqli_fetch_array($r)){
    //Pushing name and id in the blank array created 
    array_push($result,array(
        "id"=>$row['userID'],
        "name"=>$ur.$row['userPic'],
    ));
} 

//Displaying the array in json format 
echo json_encode(array('result'=>$result));
$r1= json_decode($result, true);
echo ($r1);
//  echo json_decode(array("result"=>$r1));

mysqli_close($con);
1

1 Answer 1

0

try below code

    //creating a blank array 
    $result = array();
    $r1=array();
    //looping through all the records fetched
    while($row = mysqli_fetch_array($r)){
        //Pushing name and id in the blank array created 
            array_push($result,array(
            "id"=>$row['userID'],
            "name"=>$ur.$row['userPic'],
        ));
    } 
    //Displaying the array in json format 
    $jsonData =  json_encode($result);
    echo $jsonData;  // **MODIFIED LINE**
    $r1 = json_decode($jsonData, true); // **MODIFIED LINE**
    print_r($r1);
    mysqli_close($con);
Sign up to request clarification or add additional context in comments.

2 Comments

We are not getting the result in JSON format. We are getting the same result as before and rtrim also not working
Thank you for your replay,I have found the answer '$jsonData = json_encode($result,JSON_UNESCAPED_SLASHES); echo $jsonData;'

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.