简体   繁体   中英

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.

{"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:

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' "

I saw some other similar questions in StackOverflow but the solutions didn't work. 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()

That is an Object containing properties and devices property is an object as well that contains it own properies

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. 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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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