How to convert a php array to JSON?

I have the following array and i want to convert it into json format so please help me.

<?php

   $txt=array("apple","orange","papaya");

   json_encode($txt);

?>

Output :- ["apple","orange","papaya"]

But i want the output in the following format

[{"fruit":{"1":"apple","2":"orange","3":"papaya"}}]

Please help me out , i have used json_encode but i didn't got my expected result .

So , please suggest me , what will be the possible solution ?

Thanks in Advance !!!

Answers


Simply do this:

<?php
   $txt=array(
           array("fruit"=>
                       array(1=> "apple",
                             2=>"orange",
                             3=>"papaya")
                   )
   );

   echo json_encode($txt);

?>

output:

[{"fruit":{"1":"apple","2":"orange","3":"papaya"}}]

demo


Need Your Help

Wpf - relative image source path

wpf image path

I'm having problems setting the source for images in my Wpf application. I have an Image where the source is bound to the SourceUri property of the DataContext object, like this:

Ext.Net 100% Width Layout

c# extjs asp.net-mvc-4 ext.net

I started some days ago with Ext.NET (on MVC4 site) and I created some Forms with Ext.NET but I can't set 100% width to containers.