How to refactor these if else statements to a function

user1292810

I have code as following:

$arr1 = array();
$arr2 = array();

// [...] some code

$field_name = 'foo';
if (isset($arr1[$field_name])) {
    $arr1[$field_name]++;
} else {
    $arr1[$field_name] = 1;
}

$another_field_name = 'bar';
if (isset($arr2[$another_field_name])) {
    $arr2[$another_field_name]++;
} else {
    $arr2[$another_field_name] = 1;
}

There are many more arrays and the same ifelse statements.

I would like to refactor these statements to a function:

function ifelse_arr ($arr_name, $field_name) {
    if (isset($arr_name[$field_name])) {
        $arr_name[$field_name]++;
    } else {
        $arr_name[$field_name] = 1;
    }
}

But how can I pass an array name and field name as a variable to the function?

Mikk

You don't want to pass array nor variable name, you want to pass array and variable themselves.

function ppIfIsset(array &$array, $field) {
    $array[$field] = isset($array[$field]) ? $array[$field] + 1 : 1;    
}

And call it like

ppIfIsset($arr1, 'foo');

Using & before function parameter, tells, that variable will be passed by reference

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Refactor function with for loop and multiple if else statements

From Dev

Refactor If/Else Statements

From Dev

How to correctly refactor this big and with very similar code if/else statements?

From Dev

Return a function for IF ELSE IF statements

From Dev

Refactor to smaller function, how?

From Dev

Design pattern to refactor code with nested if else and switch statements

From Dev

How to refactor long if/return statements in Ruby?

From Dev

How can I refactor a set of ugly if statements?

From Dev

How to refactor long if/return statements in Ruby?

From Dev

How to refactor consecutive, independent, if statements in this algorithm?

From Dev

How to refactor logging statements in sibling classes?

From Dev

how to refactor this multi-nested if else block

From Dev

how to use Array with if else statements?

From Dev

How do I refactor out GOTO statements in GWBASIC code?

From Dev

Is there a way to refactor these if statements?

From Dev

Defining a piecewise function with if and else statements in Rstudio

From Dev

How can I export if / else if statements into the function inside Class component? react, node, express

From Dev

How to avoid very long if-elif-elif-else statements in Python function

From Dev

How can I export if / else if statements into the function inside Class component? react, node, express

From Dev

How to stop the int main(void) printf statements when there is an else statement in another function that gives another result?

From Dev

How are if statements are excuted in a function?

From Dev

How to refactor a function that throws exceptions with Scalaz or Cats

From Dev

How to refactor each function with map in Ruby?

From Dev

How to refactor each function with map in Ruby?

From Dev

How to refactor a function replacing an argument pattern match

From Dev

How to refactor JS function according to module pattern?

From Dev

How to refactor large if else block in java servlet front controller

From Dev

How can I refactor my if-else statement?

From Dev

Refactor if-else-if structure

Related Related

  1. 1

    Refactor function with for loop and multiple if else statements

  2. 2

    Refactor If/Else Statements

  3. 3

    How to correctly refactor this big and with very similar code if/else statements?

  4. 4

    Return a function for IF ELSE IF statements

  5. 5

    Refactor to smaller function, how?

  6. 6

    Design pattern to refactor code with nested if else and switch statements

  7. 7

    How to refactor long if/return statements in Ruby?

  8. 8

    How can I refactor a set of ugly if statements?

  9. 9

    How to refactor long if/return statements in Ruby?

  10. 10

    How to refactor consecutive, independent, if statements in this algorithm?

  11. 11

    How to refactor logging statements in sibling classes?

  12. 12

    how to refactor this multi-nested if else block

  13. 13

    how to use Array with if else statements?

  14. 14

    How do I refactor out GOTO statements in GWBASIC code?

  15. 15

    Is there a way to refactor these if statements?

  16. 16

    Defining a piecewise function with if and else statements in Rstudio

  17. 17

    How can I export if / else if statements into the function inside Class component? react, node, express

  18. 18

    How to avoid very long if-elif-elif-else statements in Python function

  19. 19

    How can I export if / else if statements into the function inside Class component? react, node, express

  20. 20

    How to stop the int main(void) printf statements when there is an else statement in another function that gives another result?

  21. 21

    How are if statements are excuted in a function?

  22. 22

    How to refactor a function that throws exceptions with Scalaz or Cats

  23. 23

    How to refactor each function with map in Ruby?

  24. 24

    How to refactor each function with map in Ruby?

  25. 25

    How to refactor a function replacing an argument pattern match

  26. 26

    How to refactor JS function according to module pattern?

  27. 27

    How to refactor large if else block in java servlet front controller

  28. 28

    How can I refactor my if-else statement?

  29. 29

    Refactor if-else-if structure

HotTag

Archive