Patrick Helms Patrick Helms - 24 days ago 6
JSON Question

json_encode dropping array_values key?

I am working in PHP and have an issue with

json_encode()
. The process wipes the inital "key" that was set by the "array_values" command as shown below.

array(1) {
[0]=> array(5) {
["ID"]=> string(4) "2679" [
"PackageName"]=> "Update for Windows 8.1 for x64-based Systems (KB2965065)"
["Description"]=> string(262) "Install this update to resolve issues in Windows."
["KB"]=> string(7) "2965065"
["Repo"]=> string(21) "Windows Update Server"
}

$packages = array_values($this->control->fetchPackageTables($this->input->get("platform")));

[
{
"ID":"2679",
"PackageName":"Update for Windows 8.1 for x64-based Systems (KB2965065)",
"Description":"Install this update to resolve issues in Windows.",
"KB":"2965065",
"Repo":"Windows Update Server"
}
]


I have tried looping with a foreach through the array and setting a number but it still gets dropped. Any ideas?

Thanks!

Answer

JSON does not support associative arrays.

Arrays in JSON / Javascript consist only of numbered indexes. Associative arrays will be converted to objects/properties.

There is nothing getting dropped. [ ] indicates an array. { } indicates an object. You have one object in the array.