how to store json output into seperate PHP variables?

{"msg":"success","msg_text":{"9912345678":"N"}}

Recommended Answers

All 3 Replies

Agree with @jkon, please use the json_decode() to decode the json format into array. Then from the array, manually assign them into another variable(if it really needed).

Member Avatar

You want 'separate' variables?

The json you supply will give the following with json_decode when 2nd param set to true:

['msg' => 'success', 'msg_text' => ['9912345678' => 'N']]

If you need to extract to separate variables you can do it manually:

$msg = $array['msg'];
$msg_key = key($array['msg_text']);
$msg_value = $array['msg_text'][$msg_key];

Alternatively use a loop to get key=>value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.