Alilu TAhiru Alilu TAhiru - 9 months ago 31
PHP Question

How to use php and jquery to produce a multilevel json structure

I want to product an output like the below, but I don't know how to do it, any help?

{
"messages":[
{
"from":"InfoSMS",
"to":[
"41793026727",
"41793026731"
],
"text":"May the Force be with you!"
},
{
"from":"41793026700",
"to":"41793026785",
"text":"A long time ago, in a galaxy far, far away... It is a period of civil war. Rebel spaceships, striking from a hidden base, have won their first victory against the evil Galactic Empire."
}
]
}

Answer Source

I am assuming you are referring to how their syntax's differ. I first provide a way to define it in PHP and then I show you a way to define it in JavaScript:

<?php

$json = Array( 
   "messages" => Array(   
      Array( 
         "from" => "InfoSMS",
         "to" => Array(   
            "41793026727",
            "41793026731"
         ),
         "text" => "May the Force be with you!"
      ),
      Array(   
         "from" => "41793026700",
         "to" => "41793026785",
         "text" => "A long time ago, in a galaxy far, far away... It is a period of civil war. Rebel spaceships, striking from a hidden base, have won their first victory against the evil Galactic Empire."
      )
   )
);

print_r($json);

?>

<script>

json = {  
   "messages":[  
      {  
         "from":"InfoSMS",
         "to":[  
            "41793026727",
            "41793026731"
         ],
         "text":"May the Force be with you!"
      },
      {  
         "from":"41793026700",
         "to":"41793026785",
         "text":"A long time ago, in a galaxy far, far away... It is a period of civil war. Rebel spaceships, striking from a hidden base, have won their first victory against the evil Galactic Empire."
      }
   ]
};

console.log(json);

</script>

PHP's eval.in:

https://eval.in/753090

Javascript:

https://eval.in/753091