samedi 23 octobre 2021

Need better simpler solution for conditional link

I have three condition/variable combination e.g. below called amounts:

$a = 15000; $b = 10000; $c = 5000;
or
$a = 10000; $b = 15000; $c = 0;
or
$a = 12000; $b = 0; $c = 15000;
etc. 

At least each $a or $b or $c above is not 0 (zero).

Each condition have its own associated array e.g. :

$a_array = array('id'=>1);
$b_array = array('id'=>2);
$c_array = array('id'=>3);

If $a / $b / $c is not 0 (zero) then if $b or $c not zero it needs to be linked to $a or $b (if not zero) as below:

if($a != 0 && $b != 0 && $c != 0){
    $b_array['id_link'] = $a_array['id'];
    $c_array['id_link'] = $a_array['id'];
} elseif ($a != 0 && $b != 0 && $c == 0){
    $b_array['id_link'] = $a_array['id'];
} elseif ($a != 0 && $b == 0 && $c != 0){
    $c_array['id_link'] = $a_array['id'];
} elseif ($a == 0 && $b != 0 && $c != 0){
    $c_array['id_link'] = $b_array['id'];
}

The result for conditional statement above seems correct as you can check at the php sandbox

Is there any better idea for the conditional code and is there a missing condition (error handling). Any idea or solutions is greatly appreciated. Thanks!!

Aucun commentaire:

Enregistrer un commentaire