note 113290 added to function.json-encode
| From: | maurosys at gmail dot com | Date: | Sat, 21 Sep 2013 15:14:39 +0000 |
| Subject: | note 113290 added to function.json-encode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-196334@lists.php.net to get a copy of this message | ||
I don't understand the follow
<?
class Tipo {
public $Id;
public $Nome;
}
for ($i=0;$i<10;$i++) {
$categoria = new Tipo();
$categoria->Id = $i;
$categoria->Nome = "Categoria número $i";
$categorias[$i] = $categoria;
}
$inicio = 4;
$categorias2 = Array();
for ($i=0;$i<10;$i++) {
if ($i < $inicio) continue;
$categoria = new Tipo();
$categoria->Id = $i;
$categoria->Nome = "Categoria número $i";
$categorias2[$i] = $categoria;
}
echo "<pre>";
echo json_encode($categorias);
/*
this return
[{"Id":0,"Nome":"Categoria n\u00famero
0"},{"Id":1,"Nome":"Categoria n\u00famero
1"},{"Id":2,"Nome":"Categoria n\u00famero
2"},{"Id":3,"Nome":"Categoria n\u00famero
3"},{"Id":4,"Nome":"Categoria n\u00famero
4"},{"Id":5,"Nome":"Categoria n\u00famero
5"},{"Id":6,"Nome":"Categoria n\u00famero
6"},{"Id":7,"Nome":"Categoria n\u00famero
7"},{"Id":8,"Nome":"Categoria n\u00famero
8"},{"Id":9,"Nome":"Categoria n\u00famero 9"}]
*/
echo "<br>";
echo json_encode($categorias2);
/*
this return
{"4":{"Id":4,"Nome":"Categoria n\u00famero
4"},"5":{"Id":5,"Nome":"Categoria n\u00famero
5"},"6":{"Id":6,"Nome":"Categoria n\u00famero
6"},"7":{"Id":7,"Nome":"Categoria n\u00famero
7"},"8":{"Id":8,"Nome":"Categoria n\u00famero
8"},"9":{"Id":9,"Nome":"Categoria n\u00famero 9"}}
*/
?>
why in second call the json_encode add the variable $inicio in my array? there are some config to
do? I need always the first mode, but if I use any variable it's added in my return array.
----
Server IP: 69.147.83.199
Probable Submitter: 201.41.203.37
----
Manual Page -- http://php.net/manual/en/function.json-encode.php
Edit -- https://master.php.net/note/edit/113290
Del: integrated -- https://master.php.net/note/delete/113290/integrated
Del: useless -- https://master.php.net/note/delete/113290/useless
Del: bad code -- https://master.php.net/note/delete/113290/bad+code
Del: spam -- https://master.php.net/note/delete/113290/spam
Del: non-english -- https://master.php.net/note/delete/113290/non-english
Del: in docs -- https://master.php.net/note/delete/113290/in+docs
Del: other reasons-- https://master.php.net/note/delete/113290
Reject -- https://master.php.net/note/reject/113290
Search -- https://master.php.net/manage/user-notes.php