note 111641 added to function.json-decode
| From: | (perrito) | Date: | Mon, 11 Mar 2013 13:45:04 +0000 |
| Subject: | note 111641 added to function.json-decode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-194029@lists.php.net to get a copy of this message | ||
Array to Json, for newble will help.
<?php
//array from variable or data base
$futbol = array(
array(
"name" => "FC Barcelona - Milan, ÐаÑÑелона -
Ðилан",
"date" => "ÐÑ, 12/03/2013",
"confirm" => "s",
"time" => "20:45",
"tipo"=> "na"
),
array(
"name" => "FC Barcelona - Rayo Vallecano,
ÐаÑÑелона - Райо
ÐаленÑиано",
"date" => "СÑ, 17/03/2013",
"confirm" => "s",
"time" => "21:00",
"tipo"=> "d"
),
);
$json = '';
//first section of json
$json .= '{"footbol":[';
for ($i = 0; $i < count($futbol); $i++ ) {
if (!$i == count($futbol)){
$json .= '
{"name": "'.$futbol[$i]['name'].'","date":
"'.$futbol[$i]['date'].'","confirm":
"'.$futbol[$i]['confirm'].'","time":
"'.$futbol[$i]['time'].'","tipo":
"'.$futbol[$i]['tipo'].'"},';
}else {
$json .= '
{"name": "'.$futbol[$i]['name'].'","date":
"'.$futbol[$i]['date'].'","confirm":
"'.$futbol[$i]['confirm'].'","time":
"'.$futbol[$i]['time'].'","tipo":
"'.$futbol[$i]['tipo'].'"}';
}
}
$json .= ']}';
//just chek output
echo $json;
//try to see
var_dump(json_decode($json));
// This is a little example of How u can use Json object for output information
//json string to object
$obj = (json_decode($json));
//looping trugh object to retrive data
foreach ($obj->footbol as $match) {
echo $match->name.'<br/>';
echo $match->time.'<br/>';
echo $match->date.'<hr/>';
}
?>
----
Server IP: 69.147.83.199
Probable Submitter: 37.14.133.102
----
Manual Page -- http://php.net/manual/en/function.json-decode.php
Edit -- https://master.php.net/note/edit/111641
Del: integrated -- https://master.php.net/note/delete/111641/integrated
Del: useless -- https://master.php.net/note/delete/111641/useless
Del: bad code -- https://master.php.net/note/delete/111641/bad+code
Del: spam -- https://master.php.net/note/delete/111641/spam
Del: non-english -- https://master.php.net/note/delete/111641/non-english
Del: in docs -- https://master.php.net/note/delete/111641/in+docs
Del: other reasons-- https://master.php.net/note/delete/111641
Reject -- https://master.php.net/note/reject/111641
Search -- https://master.php.net/manage/user-notes.php