1

I don't have a specified number of subarrays, but my data structure can look like this:

array(("john","alex","tyler"),("1","2","3"),("brown","yellow","green"));

The above example has 3 subarrays, but the problem is this number can change.

As a final output I need, in this example, an array with 3 strings.

array("john,1,brown","alex,2,yellow","tyler,3,green");

I've tried to write this with functions, but couldn't find a solution.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136

4 Answers4

3

The solution using call_user_func_array, array_map and array_merge functions:

$arr = [["john","alex","tyler"],["1","2","3"],["brown","yellow","green"]];

$groups = call_user_func_array("array_map", array_merge([null], $arr));
$result = array_map(function ($v) {
    return implode(",", $v);
}, $groups);

print_r($result);

The output:

Array
(
    [0] => john,1,brown
    [1] => alex,2,yellow
    [2] => tyler,3,green
)

http://php.net/manual/en/function.call-user-func-array.php

RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
2

Simpler approach using argument unpacking (php 5.6+)

<?php

$array = array(array("john","alex","tyler"),array("1","2","3"),array("brown","yellow","green"));

$result = array_map(function(...$elements) { return implode(",", $elements); }, ...$array);

print_r($result);

Same result as other replies.


Here is what is going on in a bit more detail:

array_map(/* anonymomous function */, ...$array);

...$array takes the nested array and unpackes it, like you would call

array_map(/* anonymomous function */, $array[0], $array[1], ... $array[n])

array_map may take multiple arrays and apply the given callable to each round.

While the anonymous function more or less does the inverse

function(...$elements) { return implode(",", $elements); }

...$elements takes an arbitrary amount of parameters and turns them into one array, which is then joined into one string.

c11o
  • 152
  • 1
  • 5
  • I like this succinct answer -- it is direct and makes effective use of the splat operator. Note that from PHP7.4, the syntax can be reduced further by way of "arrow functions". `array_map(fn(...$elements) => implode(",", $elements), ...$array)` Demo: https://3v4l.org/mVIF5 – mickmackusa Jun 17 '20 at 23:00
0

Here we are changing values of an array by converting array rows into columns. then imploding each array with comma (,) Example: array[1][0] will become array[0][1] etc

PHP code demo

<?php
$array=array(
        array("john","alex","tyler"),
        array("1","2","3"),
        array("brown","yellow","green")
);
$result=array();
for($x=0;$x<count($array);$x++)
{
    for($y=0;$y<count($array[$x]);$y++)
    {
        $result[$x][$y]=$array[$y][$x];//changing array values
    }
}
foreach($result as $key => $value)
{
    $result[$key]=  implode(",", $value);//combining array values on comma(,)
}
print_r($result);

Output:

Array
(
    [0] => john,1,brown
    [1] => alex,2,yellow
    [2] => tyler,3,green
)
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
0

@ultradurable3 this can be also done array_column() here you don't have to worry about the number of sub arrays, try like below:

<?php
$array =  array(array("john","alex","tyler"), array("1","2","3"), array("brown","yellow","green"));
$finalArr = array(implode(",", array_column($array, 0)), implode(",", array_column($array, 1)), implode(",", array_column($array, 2)));
echo "<pre>";
print_r($finalArr);
lazyCoder
  • 2,544
  • 3
  • 22
  • 41