Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php-访问json数组对象_Php_Arrays_Json - Fatal编程技术网

php-访问json数组对象

php-访问json数组对象,php,arrays,json,Php,Arrays,Json,我有一个JSON数组对象,我试图在其中一个字段中附加一个数组 {"email":"bar@foo.org","password":"password","devices":{}} print_r($arr) gives me: Array ( [0] => { "email":"bar@foo.org", "password":"password", "devices":{}

我有一个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":{}
                }
    ) 
其中$device_info是一个结构数组:

array(
        "number" => $phoneNumber,
        "type" => "CellPhone",
        "config" => array(
            "batteryLevel" => 100,
            "Lowbatterylevel" => 10,
        )
我正在尝试这样做:

array_push($arr[$i]["devices"],$device_info);
这会引发错误警告:非法字符串偏移量“设备”


我在StackOverflow中看到了其他一些类似的问题,但解决方案不起作用。有人能指出我做错了什么吗?提前感谢。

您没有仔细查看原始JSON字符串或打印的完整输出

这是一个包含属性的对象,而devices属性也是一个包含自己属性的对象

下面是一些示例代码,可以帮助您继续

$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);
结果是

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}}}

对我来说,这看起来像是在你的方法中混淆了对象和数组

您发布的json编码字符串不编码数组,而是编码对象。所以你必须这样对待它。请看以下简单的演示代码:

<?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);

您需要定义索引$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);

向我们展示您的json解码语句如果您向我们展示打印的全部细节而不是摘要。答案显然是,我怀疑带有该初始字符串的print_r是否会输出一个数组…@arkascha我想他一定做了一个,以获得与他所做的输出类似的任何东西suggests@RiggsFolly在我看来,她混淆了对象和数组。
$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);
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)
      }
    }
  }
}