I have stumbled across an obscure bug which took me a long time to fix and I am vaguely curious about the underlying cause. Could someone please shed some light on the anomaly that I'm seeing when using the array_filter function instead of a simple loop.
I would have thought the following two blocks of code would produce the same result:
// original 'buggy' version
$obj_to->ExtendedProperties[] = array_filter($extendedProperties, function($item){
return isset($item);
});
// later, working version
foreach ($extendedProperties as $prop) {
if (isset($prop)) $obj_to->ExtendedProperties[] = $prop;
}
However the API to which I was then sending the results did not consider their output equivalent and returned a 400 error. I've included samples of the different json output. In one case, we see an array indice in the printed json and in the other, we don't.
// 'good' json
"ExtendedProperties": [
{
"Name": "MaterialCode",
"Value": "5450790E0LD048P8"
},
...
as opposed to
// 'bad' json
"ExtendedProperties": {
"0": {
"Name": "PreferredDate",
"Value": "2016-06-01"
},
...