简体   繁体   English

PHP多维关联数组使用json_encode()与JS对象

[英]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. 我感到困惑,无法弄清楚为什么这个JSON对象代码没有像我认为的那样出现。

For reasons not important to theis example, the PHP array is created from another array. 出于对本示例不重要的原因,PHP数组是从另一个数组创建的。 As a PHP array, this works fine. 作为PHP数组,这可以正常工作。

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_encode

$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. 您必须在第一次调用中使用JSON_FORCE_OBJECT -option,因为$json_array已经是一个字符串,所以JSON_FORCE_OBJECT在那里无效。

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] ). 当然,具有双重编码的结果变量JSFileList也将是一个字符串,当您需要使用JS编码访问对象时,它只需1次( document.write然后将显示类似[object Object] )。

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

This is an array of objects. 这是一个对象数组。 You can access the individual objects by the numeric array index, like JSFileList[2].FileName . 您可以通过数字数组索引访问单个对象,例如JSFileList[2].FileName It already works as you need it. 它已经可以根据您的需要工作。

Javascript distinguishes between continuously numerically indexed arrays ( [] ) and associative objects ( {} ), PHP does not. Javascript区分连续数字索引数组( [] )和关联对象( {} ),PHP则没有。 json_encode encodes all continuously numerically indexed PHP arrays to Javascript arrays, anything else to objects. json_encode将所有连续数字索引的PHP数组编码为Javascript数组,其他则编码为对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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