note 111641 deleted from function.json-decode by aharvey
| From: | aharvey@php.net | Date: | Mon, 11 Mar 2013 16:21:39 +0000 |
| Subject: | note 111641 deleted from function.json-decode by aharvey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-194036@lists.php.net to get a copy of this message | ||
Note Submitter: comp ( perrito ) compbcn.es
----
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/>';
}
?>