1

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.

2
  • 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. Commented Feb 3, 2014 at 6:49
  • It's really unnecessary, as json_encode will force it anyway, because it is, at lease partly, an associative array. Commented Feb 3, 2014 at 6:53

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

As did I, obviously (grin). Got lost somewhere in my experimenting ... And that was the problem.
0
[{...},{...},{...}]

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.

3 Comments

The result of JSFileList[0].FileName is [. The result of JSFileList[1].FileName is (. The result of JSFileList[2].FileName is ".
CORRECTION: The result of JSFileList[0].FileName is undefined. The result of JSFileList[0] is [ . The result of JSFileList[1] is (, etc.
Actually yes, because of stackoverflow.com/a/21521138/476. Combine that and my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.