0
$ch = curl_init($url);
$result = curl_exec($ch);

this is the result

{"response":"1", "name":"john", "id":22, "again":0, "text":xe};

Before echoing it in the js file via ajax I want to format it so I only get the data which i want to use which is id and name but its outputting the wrog data

$data = array();
$data[] = array("id"=>$result.'id', "name"=>$result.'name');
echo $data;

data it prints

( [0] => Array ( [id] => 1id and so on
1
  • See this answer. You won't need the loop but it should give you an idea of how to achieve what you want. Commented Jun 2, 2019 at 9:06

1 Answer 1

0

if you want result become an object do below

$obj = json_decode($result);

if you want resualt become an array do below

$arr = json_decode($result , true);

echo $obj->name;
echo $arr['name'];
Sign up to request clarification or add additional context in comments.

2 Comments

For some reason '$arr = json_decode($result , true);' After than echoing arr gives the output 1 print_r($arr); Same thing happens when i use $obj = json_decode($result);
what you want to echo should be as this -->echo $arr['name'];

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.