The following is an example of a multi-dimensional array that I'm working with:
array:4 [
"discount" => array:2 [
0 => 10.0
1 => 100.0
]
"updated_at" => array:2 [
0 => "2015-02-25 11:35:36"
1 => "2015-02-25 11:35:57"
]
"creator" => array:1 [
"url" => array:2 [
0 => null
1 => null
]
]
"lines" => array:2 [
0 => array:4 [
"created_at" => array:2 [
0 => "2015-02-25 11:35:36"
1 => "2015-02-25 11:35:57"
]
"updated_at" => array:2 [
0 => "2015-02-25 11:35:36"
1 => "2015-02-25 11:35:57"
]
"products" => array:1 [
0 => array:1 [
"pivot" => array:1 [
"order_line_id" => array:2 [
0 => 25
1 => 27
]
]
]
]
"sites" => array:1 [
0 => array:1 [
"pivot" => array:1 [
"order_line_id" => array:2 [
0 => 25
1 => 27
]
]
]
]
]
1 => array:4 [
"discount" => array:2 [
0 => 10.0
1 => 100.0
]
"created_at" => array:2 [
0 => "2015-02-25 11:35:36"
1 => "2015-02-25 11:35:57"
]
"updated_at" => array:2 [
0 => "2015-02-25 11:35:36"
1 => "2015-02-25 11:35:57"
]
"products" => array:2 [
0 => array:1 [
"pivot" => array:1 [
"order_line_id" => array:2 [
0 => 26
1 => 28
]
]
]
1 => array:1 [
"pivot" => array:1 [
"order_line_id" => array:2 [
0 => 26
1 => 28
]
]
]
]
]
]
]
As you can see, each key has two values, 0 and 1.
What I want to do is convert the multi-dimensional array into a string, in the format:
discount changed from 10.0 to 100.0.
updated_at changed from 2015-02-25 11:35:36 to 2015-02-25 11:35:57
...
...
lines created_at changed from xxx to yyy
lines products pivot order_line_id changed from 25 to 27.
The latter strings are based on the key names for each level of the array.
I'm nearly there, but it's not an ideal solution as it doesn't handle the level of nesting I need:
convertDifferencesArrayToString($difference)
public function convertDifferencesArrayToString($array)
{
// Nothing was changed, or only the creator timestamps were touched.
if(!$array || (sizeof($array)==1 && isset($array['creator']))){
return 'Submitted the same data';
}
$string = 'Changed ';
foreach($array as $key=>$value)
{
// Calculate the difference for standard values
if(!$this->hasNestedRelations($value))
{
$string .= $this->convertValuesToString($key, $value);
}
else
{
foreach($value as $rel_key=>$rel_val)
{
$string .= $this->convertValuesToString($rel_key, $rel_val, $key);
}
}
}
return rtrim($string, ', ');
}
private function hasNestedRelations($value)
{
if(array_key_exists(0, $value) && array_key_exists(1, $value)) {
return false;
}
return true;
}
private function convertValuesToString($key, $value, $parent_key = null)
{
// The 'creator' parent key is excluded, just in case the user is editing
// their own user account, which they made themselves.
if($parent_key) {
if($key=='name'){
return sprintf('%s%s from %s to %s, ',
$parent_key ? ucwords($parent_key) : '',
'',
(is_null($value[0]) ? '[empty]' : $value[0]),
(is_null($value[0]) ? '[empty]' : $value[1])
);
}
} else {
if($key!='updated_at' && ($value[0]!==$value[1]))
return sprintf('%s%s from %s to %s, ',
$parent_key ? ucwords(str_replace('_', ' ', $parent_key)) . ': ' : '',
ucwords(str_replace('_', ' ', $key)),
(is_null($value[0]) ? '[empty]' : $value[0]),
(is_null($value[0]) ? '[empty]' : $value[1])
);
}
}