Interesting issue with an integration between a new Salesforce application and a legacy enterprise application. The data from salesforce API was limited to 50 records at once so as a workaround the Salesforce team gave access via a SQL call which could send more records each time. Consuming the data came via GuzzleHTTP which absorbed the data from Salesforce and delivers as an object as a nested associative array. E.g.
array (
Key1 = Value1
Key2 = array (
key3 = value3
key4 = value4
)
)
which would need to be unwound into dot notation for CSV export (with headers) as
key1, key2.key3, key2.key4 value1, value3, value4
The Salesforce arrays were 3+ levels deep. Not finding a array to dot notation function, I wrote the following:
/**
* Function unwind_nested_array.
*
* Recursive function that creates delimited keys from nested associative array.
*
* @param array $data
* The many-leveled array to unwind into an array that is one level deep.
* @param string $delimiter
* The string to use separating key values (e.g. '.')
*
* @return array
* Associative array returned as dotted of nested array.
*/
function unwind_nested_array(array $data, $delimiter = '') {
$result = [];
foreach ($data as $key => $value) {
if (is_array($value)) {
$result = $result + unwind_nested_array($value, $delimiter . $key . '.');
}
else {
$result[$delimiter . $key] = $value;
}
}
return $result;
}
Tags
- Log in to post comments