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.

  • Why use force object? What you ant in JavaScript is an array of objects. Just plain old json_encode will be fine if you only call it once. – Mike Brant Feb 3 '14 at 6:49
  • It's really unnecessary, as json_encode will force it anyway, because it is, at lease partly, an associative array. – RationalRabbit Feb 3 '14 at 6:53
up vote 2 down vote accepted

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]).

  • +1 I overlooked that problem. – deceze Feb 3 '14 at 6:40
  • As did I, obviously (grin). Got lost somewhere in my experimenting ... And that was the problem. – RationalRabbit Feb 3 '14 at 6:48
[{...},{...},{...}]

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 result of JSFileList[0].FileName is [. The result of JSFileList[1].FileName is (. The result of JSFileList[2].FileName is ". – RationalRabbit Feb 3 '14 at 6:31
  • Cannot reproduce. jsfiddle.net/VQHtp – deceze Feb 3 '14 at 6:38
  • CORRECTION: The result of JSFileList[0].FileName is undefined. The result of JSFileList[0] is [ . The result of JSFileList[1] is (, etc. – RationalRabbit Feb 3 '14 at 6:40
  • Actually yes, because of stackoverflow.com/a/21521138/476. Combine that and my answer. – deceze Feb 3 '14 at 6:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.