简体   繁体   中英

PHP Multidimensional Associative Array to JS Object Using json_encode()

I am perplexed and cannot figure out why this JSON object code is not coming out like I think it should.

For reasons not important to theis example, the PHP array is created from another array. As a PHP array, this works fine.

for($i=0;$i<$FileCount;$i++)
{
    $FileList[$i] = array
    (
        "Category" => $Category, "FileName" => $FileName, "Ext" => $Ext, "Title" => $Title, "ShortName" => $ShortName
    );
}

print_r($FileList);
Array ( [0] => Array ( [Category] => AntiqueGlass [FileName] => AntiqueGlass-BlackGlassBox-B [Ext] => jpg [Title] => BlackGlassBox [ShortName] => AntiqueGlass-BlackGlassBox ) [1] => Array ( [Category] => AntiqueGlass [FileName] => AntiqueGlass-BluePicture-B [Ext] => jpg [Title] => BluePicture [ShortName] => AntiqueGlass-BluePicture ) )

I then do this, with the result of the write shown underneath

$json_array=json_encode($FileList);
echo('
<script type="text/javascript">
    var JSFileList = '.json_encode($json_array, JSON_FORCE_OBJECT).';
    document.write(JSFileList);
</script>'."\n");

[{"Category":"AntiqueGlass","FileName":"AntiqueGlass-BlackGlassBox-B","Ext":"jpg","Title":"BlackGlassBox","ShortName":"AntiqueGlass-BlackGlassBox"},{"Category":"AntiqueGlass","FileName":"AntiqueGlass-BluePicture-B","Ext":"jpg","Title":"BluePicture","ShortName":"AntiqueGlass-BluePicture"}]

I believe the result should come out something like this

{"0":("Category":"AntiqueGlass","FileName":"AntiqueGlass-BlackGlassBox-B","Ext":"jpg","Title":"BlackGlassBox","ShortName":"AntiqueGlass-BlackGlassBox")},{"1":("Category":"AntiqueGlass","FileName":"AntiqueGlass-BluePicture-B","Ext":"jpg","Title":"BluePicture","ShortName":"AntiqueGlass-BluePicture")}

The intent is to access the object like this

JSFileList[i].FileName

All my research says this should work. I am at a loss to know what I am doing wrong.

You are calling json_encode twice:

$json_array=json_encode($FileList);
//.....
json_encode($json_array, JSON_FORCE_OBJECT);

you must use the JSON_FORCE_OBJECT -option in the first call, after that $json_array already is a String, JSON_FORCE_OBJECT will have no effect there.

Of course the resulting variable JSFileList with the double-encoding will also be a string, when you need to access the object in JS encode it only 1 time( document.write then would print something like [object Object] ).

[{...},{...},{...}]

This is an array of objects. You can access the individual objects by the numeric array index, like JSFileList[2].FileName . It already works as you need it.

Javascript distinguishes between continuously numerically indexed arrays ( [] ) and associative objects ( {} ), PHP does not. json_encode encodes all continuously numerically indexed PHP arrays to Javascript arrays, anything else to objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM