Reputation: 2644
I have the following generated JSON result and would like to find out how I can separate the results into 2 variables:
JSON
{
"data": [
[
{
"source": "server1",
"host": "pc1",
"description": "SSH server is down on {HOSTNAME}",
}
],
[
{
"source": "server2",
"host": "pc2",
"description": "webapp down",
}
]
],
"error": {
"server3": "Host is not allowed to connect to this MySQL server",
"server4": "Can't connect to MySQL server",
}
}
Intended Result:
{
"data": [
{
"source": "server1",
"host": "pc1",
"description": "SSH server is down on {HOSTNAME}",
},
{
"source": "server2",
"host": "pc2",
"description": "webapp down",
}
]
}
And
{
"error": {
"server3": "Host is not allowed to connect to this MySQL server",
"server4": "Can't connect to MySQL server",
}
}
PHP Code:
<?php
include '../include/db_conn.php';
print to_json(get_all_alert());
$return = get_all_alert();
print to_json($return["data"]);
print to_json($return["error"]);
?>
The php code still prints the results combined twice. Thanks
Upvotes: 0
Views: 44
Reputation: 11859
your json data is not correct you can check here:http://json.parser.online.fr/ remove extra commas from each array part.
try this:
<?php
$data1='{
"data": [
[
{
"source": "server1",
"host": "pc1",
"description": "SSH server is down on {HOSTNAME}"
}
],
[
{
"source": "server2",
"host": "pc2",
"description": "webapp down"
}
]
],
"error": {
"server3": "Host is not allowed to connect to this MySQL server",
"server4": "Cant connect to MySQL server"
}
}';
$val_array = json_decode($data1,true);
print_r($val_array['data']);
print_r($val_array['error']);
Upvotes: 2
Reputation: 2321
Well, you've got an extra line you probably don't need.
print to_json(get_all_alert());
Remove that, and your problem is likely to go away.
Upvotes: 1