-1

i have 50 variables in my system script. All have the same kind of naming like :

$case_un
$case_deux
$case_trois
...
$case_fourthy

I need to build a expression with all of them without writing the code of 50 variables names !

As all my variables'name begin by the pattern 'case_', is there a syntaxe to make the SUM of them or another expression ?

i need to perform such thing :

   $test_of_cases = $case_un && $case_deux && $case_trois && ...........$case_fourthy ;

and also that thing :

  $total_of_sums = $sum_un + $sum_deux + ............$sum_fifthy ;

so, is there a way to code that in PHP ? In formal language, i just need to SUM all the variables which name begins by 'case_' .

My 50 variables are not storable in a array because there are from differents sources & expression result.

I'm not sure, but i thing there is a kind of expression to perform that in other language :

    LET a=SUM (`*case_*`)
    LET b=XOR (`*case_*`)
    LET c=AND (`*case_*`)

i learnt that in my old studies 30years ago.....

i hope PHP can do the same, else not, i have to write 50 lignes of code !

Best regards

amiens80
  • 21
  • 2
  • The causal relationship (are not storable in a array because...) doesn't seem to make sense, you can still add them into an array at any time. You can also make something similar (e.g. [Traversable](https://www.php.net/manual/en/class.traversable.php)) – shingo Mar 17 '23 at 10:28
  • of course i can add them manually in a array but it is the same problem : i have to write-code one by one , so if i have 50 variables, i need to push them 50 times, 50 line of codes . there is not syntaxe to push them in a array in one operation. how tu push all my $cases_ variables into an array ? – amiens80 Mar 17 '23 at 10:30
  • I just don't understand why they are not storable in an array, can you show how these variables are set? – shingo Mar 17 '23 at 10:38
  • If they are globals, you can loop through the `$GLOBALS` array and find keys' name started with "case_", but this is not efficient. – shingo Mar 17 '23 at 10:41
  • my natural language is french, not english, i will try to simply an example : – amiens80 Mar 17 '23 at 10:42
  • i will try to simply an example: my variables are : people_physic_eyes, people_physic_weight, people_physic_size, people_data_name, people_data_forname ,people_data_age . i need to compare if one of the physical variable have change. i have 50 physical variable in the same table/array where there is also 50 other variables of not-physical data (name, forname,age). all the variable are already in the same env (a array or a table or a list), but i can't separate them ! i need a syntaxe to gather only variable with the same name-pattern to do that ! so, i don't know if PHP can do that. – amiens80 Mar 17 '23 at 10:52
  • obviously it must be array $case. $case['un'] etc – Your Common Sense Mar 17 '23 at 11:26
  • $people['physic']['eyes'], $people['physic']['weight'], $people['physic']['size'], $people['data']['name'], $people['data']['forname'] ,$people['data']['age'] - WHAT's so complex with using this primitive approach which PHP is PERFECTLY capable for? – Your Common Sense Mar 17 '23 at 11:37
  • $total_of_sums = array_sum($sum); – Your Common Sense Mar 17 '23 at 11:38

1 Answers1

-1

Suppose you have a such array

$arr = [
    'people_physic_eyes' => 1,
    'people_physic_weight' => 2,
    ...
    'name' => 'james',
    'age' => 15,
    ...
];

You want to sum all the values whose key names start with "people_physic_", there are many ways to achieve this, here is one example by using array_reduce:

$sum = array_reduce(
    array_keys($arr),
    function ($sum, $key) use ($arr) {
        # if the key starts with 'people_physic_', sum up the value
        return strpos($key, 'people_physic_') === 0 ? $sum + $arr[$key] : $sum;
    },
    0
);

But this is not efficient because you have to compare all the keys on each call.

shingo
  • 18,436
  • 5
  • 23
  • 42