this is not hard, read carefully : https://laravel.com/docs/5.6/collections
Sep 13, 2018
13
Level 4
Array Flatten
I have data like this...
array:9 [▼
0 => array:1 [▼
3101625668 => 98.0
]
1 => array:1 [▼
3913126364 => 35.77
]
2 => array:1 [▼
3913058204 => 25.33
]
3 => array:1 [▼
3101372540 => 33.47
]
4 => array:1 [▼
3913752741 => 40.0
]
5 => array:1 [▼
3913120054 => 20.4
]
6 => array:1 [▼
3913998755 => 26.8
]
7 => array:1 [▼
3913861492 => 25.2
]
8 => array:1 [▼
3913881854 => 19.8
]
]
I want to flatten the array to something like this..
[$key => $value]
Please Laravel doesn't seem to be of much help.. Someone please help!
Level 22
I wrote this code for you, I tested it and it works.
Route::get('/', function () {
$myArray = [
['3101625668' => 98.0],
['3913126364' => 35.77],
['3913058204' => 25.33],
['3101372540' => 33.47],
['3913752741' => 40.0],
['3913120054' => 20.4],
['3913998755' => 26.8],
['3913861492' => 25.2]
];
$sanitizedArray = sanitizeArray($myArray);
return $sanitizedArray;
});
function sanitizeArray($array)
{
static $newArray = [];
foreach ($array as $key => $value) {
if(is_array($value)) {
sanitizeArray($value);
} else {
$newArray[$key] = $value;
}
}
return $newArray;
}
1 like
Please or to participate in this conversation.