I'm pulling data from a dozen different text fields into a single multidimensional array, but I would like for a key to only be created if there are elements to add to it. Example:
$colors = array(
'red' => $options['red_users'],
'orange' => $options['orange_users'],
'green' => $options['green_users']
);
Let's say there is no data in the 'orange_users' input field. Other than running a conditional check on each variable, e.g...
if (!$options['orange_users']) {
...how can I efficiently validate that data exists for each input field I'm pulling from?
The easiest way to do this is with a ternary if
$replacements = array(
'red' => isset($vbulletin->options['red_users'])?$vbulletin->options['red_users']:null,
'orange' => isset($vbulletin->options['orange_users'])?$vbulletin->options['orange_users']:null,
'green' => isset($vbulletin->options['green_users'])?$vbulletin->options['green_users']null
);
Then to weed out the empty values just use array_filter
$replacements = array_filter($replacements);