I have two arrays $a
and $b
, the first being the data used to populate a form, the other being the output of the form, which may have been changed by the user. We cannot assume that all inputs are present in both arrays. I want a new array which contains only those inputs which are not NULL
and which have been changed by the user.
The truth table for this looks to me like
isset($a) isset($b) output
N N NULL
N Y $b
Y N empty string
Y Y ($a != $b ? $b : NULL)
Trying to convert this to PHP code, I've derived this horrible compound ternary thing for each member mem
of the array (assumed scalar):
$changedData =
(isset($a[mem])
?
(isset($b[mem])
?
($a[mem] != $b[mem] ? $b[mem] : NULL)
:
'')
:
(isset($b[mem]) ? $b[mem] : NULL)
);
There must surely be a better way of doing this. Your advice will be greatly appreciated.