I have a json array:
$json = [];
Then in a loop I add data to it, and these fields have nested structure:
$json[] = [
"key1" => "value1",
"key2" => array(
"key3" => "value3",
"key4" => "value4",
"foo" => $bar
)
];
How can I add the key foo only if $bar is set, otherwise not add it at all?
The following doesn't work:
$json[] = [
"key1" => "value1",
"key2" => array(
"key3" => "value3",
"key4" => "value4",
"foo" => $bar ?? null
)
];
$bar is also an array if that matters