This single line would do that:
$array = array_column($array, 'plan');
The first argument is an array | The second argument is an array key.
For details, go to official documentation: https://www.php.net/manual/en/function.array-column.php.
Answer from Usman Ahmed on Stack OverflowThis single line would do that:
$array = array_column($array, 'plan');
The first argument is an array | The second argument is an array key.
For details, go to official documentation: https://www.php.net/manual/en/function.array-column.php.
Assuming this array may or may not be redundantly nested and you're unsure of how deep it goes, this should flatten it for you:
function array_flatten($array) {
if (!is_array($array)) {
return FALSE;
}
$result = array();
foreach ($array as
value) {
if (is_array($value)) {
$result = array_merge($result, array_flatten($value));
}
else {
$result[
value;
}
}
return $result;
}
I think you can use array_reduce() function. For example:
$multi= array(0 => array('t1' => 'test1'),1 => array('t2' => 'test2'),2 => array('t3' => 'test3'),3 => array('t4' => 'test4'));
$single= array_reduce($multi, 'array_merge', array());
print_r($single); //Outputs the reduced aray
You can use as follows :
$newArray = array();
foreach($arrayData as
value) {
foreach($value as $key2 => $value2) {
$newArray[$key2] = $value2;
}
}
Where $arrayData is your DB data array and $newArray will be the result.
I see you want to flatten an array to 1-D. Here is recursive iterator class you can use,
iterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator(
result = [];
foreach($iterator as $v) {
$result[] = $v;
}
print_r($result);
RecursiveArrayIterator - This iterator allows to unset and modify values and keys while iterating over Arrays and Objects in the same way as the ArrayIterator. Additionally, it is possible to iterate over the current iterator entry.
Ref.
Demo
Solution 2:-
$arr = [["4|1","4|3","4|6"],[["4|1|2","4|1|8"],["4|3|4","4|3|9"],["4|6|5","4|6|12"]]];
array_walk_recursive($arr, function($v) use(&$result){
$result[] = $v;
});
print_r($result);
Demo
Output:-
Array
(
[0] => 4|1
[1] => 4|3
[2] => 4|6
[3] => 4|1|2
[4] => 4|1|8
[5] => 4|3|4
[6] => 4|3|9
[7] => 4|6|5
[8] => 4|6|12
)
You can use array_merge().
$arrays = [["4|1","4|3","4|6"],[["4|1|2","4|1|8"],["4|3|4","4|3|9"],["4|6|5","4|6|12"]]]
$arrayMerge = []
foreach($arrays as $array)
{
$arrayMerge = array_merge($arrayMerge, $array)
}
For more info: https://www.php.net/manual/es/function.array-merge.php
PHP have a nice function for this case: array_walk_recursive:
// your multi-dimensional-array
$array = [
'item1' => 'value1',
'item2' => 'value2',
'item3' => [
'item3a' => 'value3',
],
'item4' => [
[
'item4a' => [
'value4',
'value5',
],
]
]
];
array_walk_recursive($array, function($a) use (&$new) { $new[] = $a; });
print_r($new);
output
Array
(
[0] => value1
[1] => value2
[2] => value3
[3] => value4
[4] => value5
)
You can use recusice function to convert multidimensional arrays to single array.
function filter($array)
{
static $newArray;
if (is_array($array)):
array_map('filter', $array);
else:
$newArray[] = $array;
endif;
return $newArray;
}
$array = [
'isim' => 'Şahin',
'soyisim' => 'ERSEVER',
'yabanci_dil' => [
'tr' => 'Türkçe',
],
'languages' => [
[
'php' => [
'codeigniter',
'laravel',
'symfony'
],
'javascript' => [
'vuejs',
'react' => [
'react',
'react-native'
]
]
]
]
];
print_r(filter($array));
Result:
Array
(
[0] => Şahin
[1] => ERSEVER
[2] => Türkçe
[3] => codeigniter
[4] => laravel
[5] => symfony
[6] => vuejs
[7] => react
[8] => react-native
)
array_reduce($array, 'array_merge', array())
Example:
$a = array(array(1, 2, 3), array(4, 5, 6));
$result = array_reduce($a, 'array_merge', array());
Result:
array[1, 2, 3, 4, 5, 6];
The PHP array_mergeDocs function can flatten your array:
$flat = call_user_func_array('array_merge', $array);
In case the original array has a higher depth than 2 levels, the SPL in PHP has a RecursiveArrayIterator you can use to flatten it:
$flat = iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($array)), 0);
See as well: How to Flatten a Multidimensional Array?.
Use array_column()
Returns an array of values representing a single column from the input array.
<?php
$user_array = array(
0 => array('user_id' => 1, 'name' => 'Bob'),
1 => array('user_id' => 2, 'name' => 'John'),
2 => array('user_id' => 3, 'name' => 'Mary')
);
$users = array_column($user_array, 'user_id');
print_r($users);
Output :
Array
(
[0] => 1
[1] => 2
[2] => 3
)
Where $array is the multidimensional array you provided above:
$data = array();
foreach ($array as $item) {
$data[] = $item['user_id'];
}
print_r($data);