FieldValues array with proper keys
When using the FieldValues array currently the array has to be iterated to find the proper value you wish to access. It would be better if FieldValues array had properly named keys (maybe the SystemCode?) rather than just indexed keys.
Currently we use this PHP code to iterate an array each time we look for a particular value, and it's not really an ideal or very readable:
// Using ternary operator to test for NULL date values using $condition ? $trueVal : $falseVal
foreach($contactResult[FieldValues] as $x => $x_value) {
// iterate the FieldValues array
// echo "Key=" . $x . ", Value=" . $x_value[SystemCode];
// MemberSince system code is membership initial dade, RenewalDue is membership expiration date
if ($x_value[SystemCode]=='MemberSince'){
$membershipSince = $x_value[Value] ? date("F j, Y",strtotime($x_value[Value])) : 'UNAVAILABLE';
} elseif ($x_value[SystemCode]=='RenewalDue') {
$membershipExpiry = $x_value[Value] ? date("F j, Y",strtotime($x_value[Value])) : 'UNAVAILABLE';
}
}
Accessing FieldValues[SystemCode][Value] would be much nicer so FieldValues['MemberSince'][Value] would be much more preferred.
API is already used by hundreds of organizations, so we can’t just change data structures in existing API. Please use solution suggested by Alex Sirota.
-
Alex Sirota commented
There is a more compact code sample to search multi-dimensional arrays like FieldValues:
$membershipSince = $contactResult[FieldValues][array_search('MemberSince',array_column($contactResult[FieldValues], 'SystemCode'))][Value];
$membershipSince = $membershipSince ? date("F j, Y",strtotime($membershipSince)) : 'UNAVAILABLE';
$membershipExpiry = $contactResult[FieldValues][array_search('RenewalDue',array_column($contactResult[FieldValues], 'SystemCode'))][Value];
$membershipExpiry = $membershipExpiry ? date("F j, Y",strtotime($membershipExpiry)) : 'UNAVAILABLE';
Thought this may be useful to someone. A bit more compact than the code above but functionally equivalent.