vendredi 27 septembre 2019

Create variable inside/after if statement

In JSON with key "string" and "value":

'Something xxx something' = 10
'Something yyy something' = 20
'Something xxx something' = 30
'Something zzz something' = 40

The flow I want is:

find partial string inside string
if true
get sum of all its matched string's value
if false
return 0

What I tried is

if (strpos($string, 'xxx') == false){
        $value = array_sum(array_column($arrayjson['string'],'value'));
        continue;
    }
echo $value

But, what I am getting is

100
100

Expected result is 40

Aucun commentaire:

Enregistrer un commentaire