I'm kinda new to php and mysql so this could be something as simple as a syntax error or something but I'm having no luck with using other examples.
The array in my db (JSON encoded):
["[\"option1\"=\u003E\"1\",\"someotherthing\"=\u003E\"abc\"]",""]
Fetching JSON encoded array from UserData:
if ($stmt = $con->prepare("SELECT UserData FROM users WHERE Username=?")) {
$stmt->bind_param("s", $Username);
$stmt->execute();
$result = $stmt->get_result();
$data = $result->fetch_assoc();
$json = json_decode($data['UserData']);
echo $json[0];
}
Result:
["option1"=>"1","someotherthing"=>"abc"]
Why is it that replacing "echo $json[0];" with "echo $json['option1'];" won't work, although the array is decoded?
EDIT: got it sorted! Does the trick by using the following:
if ($stmt = $con->prepare("SELECT UserData FROM users WHERE Username=?")) {
$stmt->bind_param("s", $Username);
$stmt->execute();
$result = $stmt->get_result();
$data = $result->fetch_assoc();
$json = json_decode($data['UserData']);
$array = json_decode(json_encode($json),true);
echo $array['test'];
}