Possible Duplicate:
How to generate json using php?
I want to generate a JSON file using PHP. Now I'm generating a XML file:
public function CreateXML($array){
/*$array contains several arrays and look like this:
[0] => Array( [0] => Jonas [1] => Anne [2] => Peter)
[1] => Array( [0] => Andersen [1] => Berg [2] => Jobs)
[2]......*/
$newArray = array2();
for($i = 0; $i <= count($array[0]); $i++) {
$newArray[] = array2($array[0][$i], $array[1][$i], $array[2][$i]);
}
$xmlDoc = new DOMDocument("1.0", "iso-8859-1");
$root = $xmlDoc->appendChild(
$xmlDoc->createElement("Contacts"));
foreach($newArray as $row) {
$tag = $root->appendChild(
$xmlDoc->createElement("Contact"));
$tag->appendChild(
$xmlDoc->createElement("Firstname", $row[0]));
$tag->appendChild(
$xmlDoc->createElement("Lastname", $row[1]));
.......
}
$xmlDoc->formatOutput = true;
echo $xmlDoc->saveXML();
}
//output
<Contacts>
<Contact>
<Firstname>Jonas</Firstname>
<Lastname>Andersen</Lastname>
.......
</Contact>
......
</Contacts>
So my question is, how can I get the same result but instead generate JSON? This is my first time working with JSON and I just can't get it to work.