how to get element from json_encode array response - Codeigniter

Viewed 212

I have a response from my code which looks like this

Controller

   $results = $response 
   echo $result->item;
   exit;

Response

{"item":"Samsung A","location":"Hamburg Avenue 5920"}

I want to get only item from the response..How do i achieve this ? The response is json encoded

3 Answers

You need to convert json to object, and after that you can get the element:

$respObject = json_decode($response);
echo $respObject->item;
exit;

Use json_decode the result will converted into array

$results = json_decode($response); 
print_r( $results);
exit;

json_decode function will helps.

$json_data = '{"item":"Samsung A","location":"Hamburg Avenue 5920"}';
$result = json_decode($json_data); 
echo $result->item;
Related