I have an array of arrays called user_ids:
$user_ids = Array(
[0] => Array ( [0] => 12 )
[1] => Array ( [0] => 13 )
[2] => Array ( [0] => 14 )
)
I have a function called flatten_ar that returns an array that breaks it one level:
function flatten_ar($ar,$flat_ar) {
for($i=0; $i <= count($ar); $i++) {
if(is_array($ar[$i])) {
$flat_ar = flatten_ar($ar[$i], $flat_ar);
} else {
if(isset($ar[$i])) {
$flat_ar[] = $ar[$i];
};
};
};
return $flat_ar;
};
I create a new array called $user_ids_ar that calls the function, converts the entries to strings, and prints the results:
$user_ids_ar = flatten_ar($user_ids, array());
$user_ids_ar = json_decode(json_encode($user_ids));
print_r($user_ids_ar);
$user_ids_ar = json_decode(json_encode($user_ids));
print_r($user_ids_ar);
However, I get multiple errors for each entry:
Notice: Undefined offset: 1 in C:\..\index.php on line 30
Call Stack
# Time Memory Function Location
1 0.0019 244528 {main}( ) ...\index.php:0
2 0.0066 271544 flatten_ar( ) ...\index.php:24
3 0.0067 271592 flatten_ar( ) ...\index.php:31
Followed by the output of the new array, which has done the opposite of what I wanted.
Array (
[0] => Array (
[0] => 12 )
[1] => Array (
[0] => 13 )
[2] => Array (
[0] => 14
)
)
)
The array I expected:
Array(
[0] => "12"
[1] => "13"
[2] => "14"
)
How do I get the expected array?