简体   繁体   English

php-访问json数组对象

[英]php - Accessing json array object

I have a JSON array object in which I am trying to append an array to one of the fields. 我有一个JSON数组对象,我试图在其中将数组追加到一个字段中。

{"email":"bar@foo.org","password":"password","devices":{}}

print_r($arr) gives me:

Array ( [0] => {
                "email":"bar@foo.org",
                "password":"password",
                "devices":{}
                } 
        [1] => {
                "email":"bar2@foo.org",
                "password":"password",
                "devices":{}
                }
    ) 

where $device_info is an array of structure: 其中$ device_info是结构数组:

array(
        "number" => $phoneNumber,
        "type" => "CellPhone",
        "config" => array(
            "batteryLevel" => 100,
            "Lowbatterylevel" => 10,
        )

I am trying to do this: 我正在尝试这样做:

array_push($arr[$i]["devices"],$device_info);

which throws an error "Warning: Illegal string offset 'devices' " 引发错误“警告:字符串偏移量'devices'非法”

I saw some other similar questions in StackOverflow but the solutions didn't work. 我在StackOverflow中看到了其他一些类似的问题,但是解决方案不起作用。 Can someone point out what I'm doing wrong here? 有人可以指出我在做什么错吗? Thanks in advance. 提前致谢。

You are not looking closely enough at your original JSON String or the full output from your print_r() 您没有足够仔细地查看原始JSON字符串或print_r()的完整输出

That is an Object containing properties and devices property is an object as well that contains it own properies 那是一个包含属性的对象,而devices属性也是一个包含它自己属性的对象

Here is some sample code to get your going 这是一些示例代码,助您一臂之力

$s = '{"email":"bar@foo.org","password":"password","devices":{}}';

$j = json_decode($s);

$o = new stdClass();
$o->number = 999;
$o->type = "CellPhone";
$o->config = array("batteryLevel" => 100,"Lowbatterylevel" => 10);

$j->devices = $o;
print_r($j);
echo json_encode($j);

Results are 结果是

stdClass Object
(
    [email] => bar@foo.org
    [password] => password
    [devices] => stdClass Object
        (
            [number] => 999
            [type] => CellPhone
            [config] => Array
                (
                    [batteryLevel] => 100
                    [Lowbatterylevel] => 10
                )

        )

)
{"email":"bar@foo.org","password":"password","devices":{"number":999,"type":"CellPhone","config":{"batteryLevel":100,"Lowbatterylevel":10}}}

To me this looks like you confuse objects and arrays in your approach... 对我来说,这似乎使您混淆了方法中的对象和数组...

That json encoded string you posted does not encode an array but an object. 您发布的json编码字符串不对数组进行编码,而是对对象进行编码。 So you have to treat it as such. 因此,您必须这样对待它。 Take a look at this simple demonstration code: 看一下这个简单的演示代码:

<?php
$payload = [
    "number" => '01234567890',
    "type" => "CellPhone",
    "config" => [
        "batteryLevel" => 100,
        "Lowbatterylevel" => 10
    ]
];
$input = '{"email":"bar@foo.org","password":"password","devices":{}}';
$data = json_decode($input);
$data->devices = $payload;
$output = json_encode($data);
print_r(json_decode($output));
print_r($output);

The output ob above obviously is: 上面的输出ob显然是:

stdClass Object
(
    [email] => bar@foo.org
    [password] => password
    [devices] => stdClass Object
        (
            [number] => 01234567890
            [type] => CellPhone
            [config] => stdClass Object
                (
                    [batteryLevel] => 100
                    [Lowbatterylevel] => 10
                )

        )

)
{"email":"bar@foo.org","password":"password","devices":{"number":"01234567890","type":"CellPhone","config":{"batteryLevel":100,"Lowbatterylevel":10}}}

You need to define the index $i 您需要定义索引$ i

$users = array (
    [
     "email"=>"user@user.com",
     "device"=> array()
    ],
    [
     "email"=>"user@user.com",
     "device"=> array()
    ],
);

$device_info = array(
    "type" => "CellPhone",
    "config" => array(
        "batteryLevel" => 100,
        "Lowbatterylevel" => 10,
    )
);

for ($i=0;$i<count($users);$i++) {
    $users[$i]['device'] = $device_info;
}

var_dump($users);

And you will have the result below 您将得到以下结果

array(2) {
  [0]=>
  array(2) {
    ["email"]=>
    string(13) "user@user.com"
    ["device"]=>
    array(2) {
      ["type"]=>
      string(9) "CellPhone"
      ["config"]=>
      array(2) {
        ["batteryLevel"]=>
        int(100)
        ["Lowbatterylevel"]=>
        int(10)
      }
    }
  }
  [1]=>
  array(2) {
    ["email"]=>
    string(13) "user@user.com"
    ["device"]=>
    array(2) {
      ["type"]=>
      string(9) "CellPhone"
      ["config"]=>
      array(2) {
        ["batteryLevel"]=>
        int(100)
        ["Lowbatterylevel"]=>
        int(10)
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM