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
Answer from Azeez Kallayi on Stack OverflowI 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.
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.
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;
}
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);
<?php
$array = array(
'one' => 'one_value',
'two' => array
(
'four' => 'four_value',
'five' => 'five_value'
),
'three' => array
(
'six' => array
(
'seven' => 'seven_value'
)
)
);
function flatten($array, $prefix = '') {
$arr = array();
foreach($array as $k => $v) {
if(is_array($v)) {
$arr = array_merge($arr, flatten($v, $prefix . $k . '-'));
}
else{
$arr[$prefix . $k] = $v;
}
}
return $arr;
}
var_dump(flatten($array));
//output:
//array(4) {
// ["one"]=>
// string(9) "one_value"
// ["two-four"]=>
// string(10) "four_value"
// ["two-five"]=>
// string(10) "five_value"
// ["three-six-seven"]=>
// string(11) "seven_value"
//}
Running example
You can implement recursive processing with a stack:
$separator = '-';
$flat = array();
while ($array);
{
$key = key($array);
$value = array_shift($array);
if (is_array($value))
{
foreach($value as $subKey => $node)
{
$array[$key.$separator.$subKey] = $node;
}
}
else
{
$flat[$key] = $value;
}
}
Output (Demo):
Array
(
[one] => one_value
[two-four] => four_value
[two-five] => five_value
[three-six-seven] => seven_value
)
Easy:
$flattened = [];
foreach ($mdarray as $arr) {
foreach ($arr as $val) {
$flattened[] = $val;
}
}
What the above code does is it iterates through the multi-dimensional array, gets the value of each subarray, and adds its elements to $flattened.
If you're using PHP 5.6+, this code is simpler:
$flattened = array_merge(...$mdarray);
call_user_func_array To combine several results .
$oneDimensionalArray = call_user_func_array('array_merge',$day);
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?.
It looks like you want all sub array values to be in the single array.
$singleArray = [];
foreach($multiarray as $array) {
$singleArray = array_merge($singleArray, array_values($array));
}
This may contain some values as a duplicate. To clean them up you can do
$uniqueValues = array_unique($singleArray);
You can use this function to convert nested array to one array.
<?php
$a = ["a","b","c",["d","e",["f","g"]],["p","q","r"],["s","t","u"]];
function convert(array $array){
$arr = [];
foreach ($array as $item) {
if(is_array($item)){
$arr = array_merge($arr, convert($item));
}else {
$arr[] = $item;
}
}
return $arr;
}
echo "<pre>";
print_r(convert($a));
echo "</pre>";
?>
As long as 0 is the key name and 1 is the value, then just create a single dimension indexed on the first element and build the query string:
$string = http_build_query(array_column($array, 1, 0));
You need to rearrange the input array a bit and then you can use http_build_query()
$sq = array(array("Color", "red" ), array("Ram", "4GB" ) );
$aa = [];
foreach( $sq as $s ) {
$aa[$s[0]] = $s[1];
}
echo http_build_query($aa);
RESULT
Color=red&Ram=4GB