使用嵌套数组将PHP转换为JSON

时间:2021-08-16 21:32:23

I have a PHP variable I need to convert to JSON string.

我有一个PHP变量,我需要转换为JSON字符串。

I have following PHP code:

我有以下PHP代码:

$username="admin";
$password="p4ssword";
$name="Administrator";
$email="myname@smsfree4all.com" 
$params=compact('username', 'password','name','email', 'groups');
json_encode($params);

This works fine. But what I am not sure about is how do I encode the properties in PHP with nested key value pairs shown below:

这很好用。但我不确定的是如何使用如下所示的嵌套键值对编码PHP中的属性:

{
"username": "admin",
"password": "p4ssword",
"name": "Administrator",
"email": "admin@example.com",
"properties": {
    "property": [
        {
            "@key": "console.rows_per_page",
            "@value": "user-summary=8"
        },
        {
            "@key": "console.order",
            "@value": "session-summary=1"
        }
    ]
   }
}

What is this with @ before key value?

在键值之前@的含义是什么?

3 个解决方案

#1


1  

Something like this should do it

这样的事情应该做到这一点

$username="admin"; //more variables
$params=compact('username' /* more variables to be compacted here*/);

$params["properties"] = [ 
  "property" => [
    [
        "@key" => "console.rows_per_page",
        "@value"=> "user-summary=8"
    ],
    [ 
        "@key"=> "console.order",
        "@value"=> "session-summary=1"
    ]
  ]
];

echo json_encode($params);

The manual has more examples you can use

手册中有更多可以使用的示例

Notice that:

请注意:

  • A key~value array is encoded into an object
  • key_value数组被编码到一个对象中
  • A regular array (array of arrays here) is encoded into an array
  • 常规数组(此处数组数组)被编码为数组

Those are all the rules you need to consider to encode any arbitrary object

这些是编码任意对象时需要考虑的所有规则

#2


1  

Something like this perhaps?

也许这样的事情?

$properties = [
    'property' => [
        ['@key' => 'console.rows_per_page', '@value' => 'user-summary=8'],
        ['@key' => 'console.order', '@value' => 'session-summary=1']
    ]
];

It's difficult to tell what you're asking.

很难说出你在问什么。

#3


0  

You can nest in PHP using simple arrays, very similar to JavaScript objects:

您可以使用简单数组嵌套在PHP中,与JavaScript对象非常相似:

$grandparent = array(
  "person1" => array(
    "name" => "Jeff",
    "children" => array(
      array("name" => "Matt"),
      array("name" => "Bob")
    )
  ), 
  "person2" => array(
    "name" => "Dillan",
    "children" => array()
  )
);

#1


1  

Something like this should do it

这样的事情应该做到这一点

$username="admin"; //more variables
$params=compact('username' /* more variables to be compacted here*/);

$params["properties"] = [ 
  "property" => [
    [
        "@key" => "console.rows_per_page",
        "@value"=> "user-summary=8"
    ],
    [ 
        "@key"=> "console.order",
        "@value"=> "session-summary=1"
    ]
  ]
];

echo json_encode($params);

The manual has more examples you can use

手册中有更多可以使用的示例

Notice that:

请注意:

  • A key~value array is encoded into an object
  • key_value数组被编码到一个对象中
  • A regular array (array of arrays here) is encoded into an array
  • 常规数组(此处数组数组)被编码为数组

Those are all the rules you need to consider to encode any arbitrary object

这些是编码任意对象时需要考虑的所有规则

#2


1  

Something like this perhaps?

也许这样的事情?

$properties = [
    'property' => [
        ['@key' => 'console.rows_per_page', '@value' => 'user-summary=8'],
        ['@key' => 'console.order', '@value' => 'session-summary=1']
    ]
];

It's difficult to tell what you're asking.

很难说出你在问什么。

#3


0  

You can nest in PHP using simple arrays, very similar to JavaScript objects:

您可以使用简单数组嵌套在PHP中,与JavaScript对象非常相似:

$grandparent = array(
  "person1" => array(
    "name" => "Jeff",
    "children" => array(
      array("name" => "Matt"),
      array("name" => "Bob")
    )
  ), 
  "person2" => array(
    "name" => "Dillan",
    "children" => array()
  )
);