Append to array if not empty [on hold]











up vote
-2
down vote

favorite












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).










share|improve this question















put on hold as off-topic by 200_success, Toby Speight, Mast, Your Common Sense, vnp Nov 21 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




    would array_values(array_filter($conditions)) help? I don't really get why array keys are that important for you though
    – Your Common Sense
    Nov 21 at 15:39












  • @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.
    – Erebus
    Nov 21 at 16:08










  • You want to keep empty keys and in same time drop them? That's totally unclear.
    – Calak
    Nov 21 at 16:19










  • @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.
    – Erebus
    Nov 21 at 16:31










  • @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.
    – Calak
    Nov 21 at 16:31















up vote
-2
down vote

favorite












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).










share|improve this question















put on hold as off-topic by 200_success, Toby Speight, Mast, Your Common Sense, vnp Nov 21 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




    would array_values(array_filter($conditions)) help? I don't really get why array keys are that important for you though
    – Your Common Sense
    Nov 21 at 15:39












  • @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.
    – Erebus
    Nov 21 at 16:08










  • You want to keep empty keys and in same time drop them? That's totally unclear.
    – Calak
    Nov 21 at 16:19










  • @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.
    – Erebus
    Nov 21 at 16:31










  • @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.
    – Calak
    Nov 21 at 16:31













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











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).










share|improve this question















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).







php array






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 16:11

























asked Nov 21 at 15:31









Erebus

1215




1215




put on hold as off-topic by 200_success, Toby Speight, Mast, Your Common Sense, vnp Nov 21 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.




put on hold as off-topic by 200_success, Toby Speight, Mast, Your Common Sense, vnp Nov 21 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




    would array_values(array_filter($conditions)) help? I don't really get why array keys are that important for you though
    – Your Common Sense
    Nov 21 at 15:39












  • @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.
    – Erebus
    Nov 21 at 16:08










  • You want to keep empty keys and in same time drop them? That's totally unclear.
    – Calak
    Nov 21 at 16:19










  • @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.
    – Erebus
    Nov 21 at 16:31










  • @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.
    – Calak
    Nov 21 at 16:31














  • 2




    would array_values(array_filter($conditions)) help? I don't really get why array keys are that important for you though
    – Your Common Sense
    Nov 21 at 15:39












  • @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.
    – Erebus
    Nov 21 at 16:08










  • You want to keep empty keys and in same time drop them? That's totally unclear.
    – Calak
    Nov 21 at 16:19










  • @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.
    – Erebus
    Nov 21 at 16:31










  • @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.
    – Calak
    Nov 21 at 16:31








2




2




would array_values(array_filter($conditions)) help? I don't really get why array keys are that important for you though
– Your Common Sense
Nov 21 at 15:39






would array_values(array_filter($conditions)) help? I don't really get why array keys are that important for you though
– Your Common Sense
Nov 21 at 15:39














@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.
– Erebus
Nov 21 at 16:08




@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.
– Erebus
Nov 21 at 16:08












You want to keep empty keys and in same time drop them? That's totally unclear.
– Calak
Nov 21 at 16:19




You want to keep empty keys and in same time drop them? That's totally unclear.
– Calak
Nov 21 at 16:19












@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.
– Erebus
Nov 21 at 16:31




@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.
– Erebus
Nov 21 at 16:31












@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.
– Calak
Nov 21 at 16:31




@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.
– Calak
Nov 21 at 16:31










1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










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']));





share|improve this answer























  • Thanks, but as I mentioned in the question, array_filter doesn't work as it retains keys.
    – Erebus
    Nov 21 at 16:12










  • Just use array_values as @YourCommonSense stated
    – Calak
    Nov 21 at 16:17


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










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']));





share|improve this answer























  • Thanks, but as I mentioned in the question, array_filter doesn't work as it retains keys.
    – Erebus
    Nov 21 at 16:12










  • Just use array_values as @YourCommonSense stated
    – Calak
    Nov 21 at 16:17















up vote
1
down vote



accepted










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']));





share|improve this answer























  • Thanks, but as I mentioned in the question, array_filter doesn't work as it retains keys.
    – Erebus
    Nov 21 at 16:12










  • Just use array_values as @YourCommonSense stated
    – Calak
    Nov 21 at 16:17













up vote
1
down vote



accepted







up vote
1
down vote



accepted






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']));





share|improve this answer














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']));






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 at 16:36

























answered Nov 21 at 16:07









Calak

1,852314




1,852314












  • Thanks, but as I mentioned in the question, array_filter doesn't work as it retains keys.
    – Erebus
    Nov 21 at 16:12










  • Just use array_values as @YourCommonSense stated
    – Calak
    Nov 21 at 16:17


















  • Thanks, but as I mentioned in the question, array_filter doesn't work as it retains keys.
    – Erebus
    Nov 21 at 16:12










  • Just use array_values as @YourCommonSense stated
    – Calak
    Nov 21 at 16:17
















Thanks, but as I mentioned in the question, array_filter doesn't work as it retains keys.
– Erebus
Nov 21 at 16:12




Thanks, but as I mentioned in the question, array_filter doesn't work as it retains keys.
– Erebus
Nov 21 at 16:12












Just use array_values as @YourCommonSense stated
– Calak
Nov 21 at 16:17




Just use array_values as @YourCommonSense stated
– Calak
Nov 21 at 16:17



Popular posts from this blog

Morgemoulin

Scott Moir

Souastre