Two dimentional array into single dimentional array without foreach PHP [duplicate] - php

This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 4 years ago.
Is there way to convert two dimentional array into single dimentional array without using foreach loop in php.
Below is the actual array
Array
(
[0] => Array
(
[male] => male
[female] => female
)
[1] => Array
(
[male] => male1
[female] => female1
)
)
And Output will be like
Array
(
[0] = > male
[1] = > female
[2] = > male1
[3] = > female1
)

You can use reduce and use array_merge
$array = array( ... ); //Your array here
$result = array_reduce($array, function($c,$v){
return array_merge(array_values($c),array_values($v));
}, array());
This will result to:
Array
(
[0] => male
[1] => female
[2] => male1
[3] => female1
)

This loops through your multidimensional array and stores the results in the new array variable $newArray.
$newArray = array();
foreach($multi as $array) {
foreach($array as $k=>$v) {
$newArray[$k] = $v;
}
}

Related

Create multidimensional array from multiple arrays in PHP [duplicate]

This question already has answers here:
Merge two arrays as key value pairs in PHP
(3 answers)
Closed 5 months ago.
I have two arrays:
Array ( [0] => label [1] => data )
Array ( [0] => 1 [1] => 2 )
And I need merge them in a array like this:
Array ( [0] => Array ( [label] => 1 [data] => 2 ) )
I have tried:
for ($i=0; $i < count($inputs); $i++) {
$new = array($cols[$i] => $inputs[$i]);
$data[] = $new;
}
Any help is welcome ;)
You can simply use array_combine:
Creates an array by using one array for keys and another for its
values
$arr1 = array(0 => 'label', 1 => 'data');
$arr2 = array(0 => 1, 1 => 2);
$arr3 = array_combine($arr1, $arr2);
print_r($arr3);
Result:
Array
(
[label] => 1
[data] => 2
)
Try it
You can do it like so if you want to use a loop
$array1 = ['label', 'data'];
$array2 = [1, 2];
$array_merged = [];
foreach($array1 as $key => $value) {
$array_merged[$value] = $array2[$key];
}
var_dump($array_merged);
http://sandbox.onlinephpfunctions.com/code/c4e5bc71df53ebdafb0a54d43c3eadb4ea4cd241

How to add comma between elements of array [duplicate]

This question already has answers here:
How to implode subarrays in a 2-dimensional array?
(4 answers)
Closed 5 years ago.
everyone.
I have an output of multidimensional array(WRONG):
[ [23], [145], [16], [2], [2], [3], [], ]
I need to have an output like this(GOOD):
[ [2,3], [1,4,5], [1,6], [2], [2], [3], [], ]
Here's array of my data: Array ( [0] => Array ( [0] => 2 [1] => 3 ) [1] => Array ( [0] => 1 [1] => 4 [2] => 5 ) [2] => Array ( [0] => 1 [1] => 6 ) [3] => Array ( [0] => 2 ) [4] => Array ( [0] => 2 ) [5] => Array ( [0] => 3 ) [6] => Array ( ) )
The way I'm trying to do that:
print_r($gretimumosarasas);
echo "[ ";
for ($row = 0; $row < count($gretimumosarasas); $row++) {
echo "[";
for ($col = 0; $col < count($gretimumosarasas[$row]); $col++) {
echo $gretimumosarasas[$row][$col];
}
echo "], ";
}
echo " ]";
Can someone please explain, how to add comma like I wrote an output example below?
If there only way to add coma in second for loop and then search for last symbol index and delete it?
THANK YOU.
While I am extremely nervous of the fact you appear to generating code from your code (here be dragons!) its simple enough.
function array_formatter($arr)
{
$temp=array();
foreach ($arr as $v) {
$temp[] = is_array($v) ? array_formatter($v) : $v;
}
return '[' . implode(',', $temp) . ']';
}
BTW PHP arrays are not multi-dimensional, they are nested. Nested arrays can emulate multi-dimensional arrays, but your example data is not multi-dimensional itself.

Merging two associative arrays [duplicate]

This question already has answers here:
Restructure multidimensional array of column data into multidimensional array of row data [duplicate]
(3 answers)
Closed 5 years ago.
I have two arrays.
$a=array('caption'=>array('c one','c two'));
$b=array('photos'=>array('photo one','photo two'));
I want to merge into
Array (
[0] => Array (
[caption] => c one
[file] => photo one
)
[1] => Array (
[caption] => c two
[file] => photo two
)
)
or merge into
Array (
[0] => Array (
[0] => c one
[1] => photo one
)
[1] => Array (
[0] => c two
[1] => photo two
)
)
How do you do it?
The last case can be done by array_map function
$res = array_map(null, $a['caption'], $b['photos']);
demo
You can use a nested loop, and just keep track of your keys.
foreach ([$a, $b] as $array) {
foreach ($array as $text_key => $values) {
foreach ($values as $numeric_key => $value) {
$result[$numeric_key][$text_key] = $value;
}
}
}
If the arrays does not have more in common than the key values then this should work.
It loops one array and uses the key to grab the value from the other array.
$a=array('caption'=>array('c one','c two'));
$b=array('photos'=>array('photo one','photo two'));
Foreach($a['caption'] as $key => $capt){
$new[$key]['caption'] = $capt;
$new[$key]['photos'] = $b['photos'][$key];
}
Var_dump($new);
Output:
array(2) {
[0] => array(2) {
["caption"] => "c one"
["photos"] => "photo one"
}
[1] => array(2) {
["caption"] => "c two"
["photos"] => "photo two"
}
}
https://3v4l.org/kfsft

Concatenate values of two arrays [duplicate]

This question already has answers here:
Cartesian Product of N arrays
(10 answers)
Closed 8 years ago.
Let's assume I have an two arrays and I want to merge every value with the other value of the array.
Array 1
array (size=2)
0 => 1
1 => 2
Array 2
array (size=2)
0 => 3
1 => 4
Wanted result array / string:
array (size=4)
0 => '1,3'
1 => '1,4'
2 => '2,3'
3 => '2,4'
I can't get my head around it. Obviously I would need to merge every one array key/value with the other ones. Is there a more elegant way then doing this in a while/foreach loop?
You need a foreach loop inside a foreach loop. (Actualy, you will have to loop through both arrays to get a concatenated product of both arrays, you don't actually need two foreach loops). You could mix: whiles, foreach, for, or php filter/intersect array functions
Example
$array1 = array(1,2);
$array2 = array(3,4);
$result = array();
foreach ($array1 as $item1){
foreach($array2 as $item2){
$result[] = $item1.','.$item2;
}
}
https://eval.in/215001
your result array Length will be array1.Length * array2.Length
2d arrays
You could also put an array inside an array like this:
$array1 = array(1,2);
$array2 = array(3,4);
$result = array();
foreach ($array1 as $item1){
foreach($array2 as $item2){
$result[] = array($item1, $item2);
}
}
//$result[0][0] = 1 -- $result[0][1] = 3
//$result[1][0] = 1 -- $result[1][1] = 4
//$result[2][0] = 2 -- $result[2][1] = 3
//$result[3][0] = 2 -- $result[3][1] = 4
We call this a 2d (2 dimensional) array, because you could grapicly display this as a grid, like displayed here above. If you would put an Array, inside an Array inside an Array, you would call this a 3 dimensional array, etc.
print_r($result); in php:
Array
(
[0] => Array
(
[0] => 1
[1] => 3
)
[1] => Array
(
[0] => 1
[1] => 4
)
[2] => Array
(
[0] => 2
[1] => 3
)
[3] => Array
(
[0] => 2
[1] => 4
)
)
try
$a= array ('0' => 1,'1' => 2);
$b= array ('0' => 3,'1' => 4);
for($i=0; $i<count($a); $i++) {
for($j=0; $j<count($b); $j++) {
$newarr[]= $a[$i].','.$b[$j];
}
}
print_r($newarr);//Array ( [0] => 1,3 [1] => 1,4 [2] => 2,3 [3] => 2,4 )
$a=array('1','2');
$b=array('3','4');
$res=array();
for($i=0;$i<count($a);$i++)
{
foreach($b as $bb)
{
$res[]=strval($a[$i].','.$bb);
}
}
print_r($res);//output=Array ( [0] => 1,3 [1] => 1,4 [2] => 2,3 [3] => 2,4 )

Create an array of inner array values with array function in PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to “flatten” a multi-dimensional array to simple one in PHP?
How to create an array of inner array values with array function ?
Here is array
Array
(
[0] => Array
(
[Detail] => Array
(
[detail_id] => 1
)
)
[1] => Array
(
[Detail] => Array
(
[detail_id] => 4
)
)
)
I want create an array with detail_id of above array i.e
array(1, 4)
Is it done any array function in PHP ?
There isn't any one function which can do this single-handed. array_map is the closest you'd get, but it doesn't really deal with the extra recursion level. Why don't you just use loops?
$new_array = array();
foreach($main_array as $key => $second_array)
{
foreach($second_array as $second_key => $third_array)
{
$new_array[] = $third_array['detail_id'];
}
}
echo implode(',',$new_array);
If your are looking for a one liner:
$val = array(
array('Detail' => array('detail_id' => 1)),
array('Detail' => array('detail_id' => 4)),
);
var_dump($val);
I am looking for answer like this
$result = array_map( function($a) {
return $a['Detail']['detail_id']; }, $mainArray
);
This works correct..

Categories