I would like to extend an assoc array. The original array is like
$arr = [[
'ID' => 0,
'TEXT' => SOME_CONST,
'CHECKED' => $opt_flag,
]];
With a new condition I would like to extend this existing array.
if (defined('MODULE_STATUS') && MODULE_STATUS == 'true') {
$arr['JSON_ATTRDATA'] = 1;
}
The result is
Array
(
[0] => Array
(
[ID] => 0
[TEXT] => text
[CHECKED] => 1
)
[JSON_ATTRDATA] => 1
)
What is wrong that the new key is outside of the nestet array?
you want $arr[0]['JSON_ATTRDATA'] = 1;
right now you appending to the parent array, you want the child array which has the key 0
the Array $arr
already having 1 object
to change this object value by its index
$arr[0] = "anything else"
and to add more objects
$arr[] = "Another object";
you should Call :
$arr[0]['JSON_ATTRDATA'] = 1;
will add another key => value
to first object
Do $arr[0]['JSON_ATTRDATA']
DETAILS
$arr
contains array within array. Like this
Array
(
[0] => Array
(
[ID] => 0
[TEXT] => text
[CHECKED] => 1
)
)
So when you add $arr['JSON_ATTRDATA']
it add into first array