سؤال

How to put below value in PHP:

{     "aps": 
          {          "badge": 10,          
                     "alert": "Hello world!",         
                     "sound": "cat.caf"     
          },    
      "job_id": 1 
}
هل كانت مفيدة؟

المحلول

In PHP you will build your array like this

$data =  array( 'aps'=>array(
                     'badge'=> 10,          
                     'alert'=> 'Hello world!',         
                     'sound'=> 'cat.caf'                           
                       ),
                'job_id'=> 1
              );

echo json_encode($data);

Output

{
    "aps": {
        "badge": 10,
        "alert": "Hello world!",
        "sound": "cat.caf"
    },
    "job_id": 1
}

Demo

نصائح أخرى

You could try something like this:

<?
  $json = <<<JSON
{ "aps": { "badge": 10,
"alert": "Hello world!",
"sound": "cat.caf"
},
"job_id": 1 }
JSON;

  $data = json_decode($json,true);
?>

You can access your data like $data['aps']['badge']; or $data['job_id'];.

Let me know if this is what you need.

It's like JSON. You can use 2 functions: JSON encode and JSON decode

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top