I want to call a series of functions to build up an array, like so:

$array['conditions'][] = $this->function1($input1);
$array['conditions'][] = $this->function2($input2);
$array['conditions'][] = $this->function3($input3);

However, each function may return either an array with values or an empty array (depending on input). In the case of a returned empty array, $array['conditions'] is polluted with a bunch of empty entries, which is a problem when it comes to unit test maintenance - if I add additional functions I have to go back and update the expected value for all of my tests.

I could do something like:

$function1_return = $this->function1($input1);
if(!empty($function1_return) {
  $array['conditions'][] = $function1_return;

but I'm hoping there is a cleaner way? I also tried to remove any empty values after the fact, but to no avail (array_filter, for example, retains any array keys).


closed as off-topic by 200_success, Toby Speight, Mast, Your Common Sense, vnp Nov 21 '18 at 22:42

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – 200_success, Toby Speight, Mast, Your Common Sense, vnp
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    \$\begingroup\$ would array_values(array_filter($conditions)) help? I don't really get why array keys are that important for you though \$\endgroup\$ – Your Common Sense Nov 21 '18 at 15:39
  • \$\begingroup\$ @YourCommonSense I don't see how your proposed code solution will help, because as mentioned array_filter retains keys. The reason it's a problem is for the maintenance of tests - I have to keep adding extra empty keys to the expected array for each test as I add functions in the future. I will update my question to reflect this. \$\endgroup\$ – Erebus Nov 21 '18 at 16:08
  • \$\begingroup\$ You want to keep empty keys and in same time drop them? That's totally unclear. \$\endgroup\$ – Calak Nov 21 '18 at 16:19
  • \$\begingroup\$ @YourCommonSense jumped to a bad conclusion, apologies. Your code does indeed work perfectly! Please submit it as an answer and I will mark it as so. \$\endgroup\$ – Erebus Nov 21 '18 at 16:31
  • \$\begingroup\$ @Erebus 1) It's unclear what you asked, 2) The code you presented in its current form is not meaningfully reviewable. We only review real, working code. If you edit your question to contain your actual code we can review it for improvements. See What topics can I ask about? for reference. \$\endgroup\$ – Calak Nov 21 '18 at 16:31

The cleanest way is how you do and then filter:

$array['conditions'] = array_filter ($array['conditions']);

Or more explicitly (depend of your values):

$array['conditions'] = array_filter ($array['conditions'], function ($v){return !empty($v);});

And for normalizing indexes:

$array['conditions'] = array_values(array_filter($array['conditions']));
  • \$\begingroup\$ Thanks, but as I mentioned in the question, array_filter doesn't work as it retains keys. \$\endgroup\$ – Erebus Nov 21 '18 at 16:12
  • \$\begingroup\$ Just use array_values as @YourCommonSense stated \$\endgroup\$ – Calak Nov 21 '18 at 16:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.