"val"); but I got an error...">

Add coupled pair to PHP array

I have an array, I tried to write

array_push($json['Request']['Header'], "key" => "val");

but I got an error. The spelling below works, but it adds an array, not just the / val switch

array_push($json['Request']['Header'], array("key" => "val"));

..
[0] => Array
        (
            [key] => val
        )

//i would like
...
[key] => val
+3
source share
3 answers

Why not just write:

$json['Request']['Header'] = array();
$json['Request']['Header']['key'] = 'val';
+6
source

Try

$json['Request']['Header']['key'] = 'val';
+2
source

Use the add operator to add an associative array:

$json['Request']['Header'] += array("key" => "val");
+1
source

Source: https://habr.com/ru/post/1719394/


All Articles