1

I have an array with arrays in it representing values in a database.

There are over 100 columns in the db table so the actual count is much higher than this example below of 6 values, the sub-array (Array within the array) index 0-5.

The columns are in each index of the sub-array and the rows are in each index of the main array.

Here is my main array with sub-arrays:

Array
(
    [0] => Array
        (
            [0] => N
            [1] => N
            [2] => Y
            [3] => Y
            [4] => Y
            [5] => Y
)
 [1] => Array
        (
            [0] => N
            [1] => N
            [2] => Y
            [3] => Y
            [4] => N
            [5] => Y
)
[2] => Array
        (
            [0] => N
            [1] => N
            [2] => Y
            [3] => Y
            [4] => N
            [5] => Y
)
[3] => Array
        (
            [0] => Y
            [1] => Y
            [2] => Y
            [3] => Y
            [4] => Y
            [5] => Y
)

What I need to do is concat all the values of each sub index into one array like this:

Array
( 
            [0] => N,N,N,Y
            [1] => N,N,N,Y
            [2] => Y,Y,Y,Y
            [3] => Y,Y,Y,Y
            [4] => Y,N,N,Y
            [5] => Y,Y,Y,Y
)

There will always be the same number of columns (sub index) but there will be different amounts of rows (index).

Yourguide
  • 109
  • 9
  • As demonstrated by sevavietl's answer, this is a simple task of [transposing an array](https://stackoverflow.com/q/797251/2943403). The fact that comma-separated strings are desired is a supplemental step. – mickmackusa Jul 25 '22 at 09:15
  • Duplicate with worse question details and worse answers versus this page: https://stackoverflow.com/q/7074080/2943403 – mickmackusa Jul 25 '22 at 10:13

4 Answers4

2

The idea is get the data by column, you're in luck, there's a built in function for that. Its array_column.

So first, get the number of columns and simply use a for loop for that. Then just use implode and assign it inside a new container:

$new = array(); // container
$count = count($arr[0]); // get the number of colums
for ($i = 0; $i < $count; $i++) {
    // get the data by column number ($i), then implode and push inside
    $new[] = implode(',', array_column($arr, $i));
}

Here's a sample output

Kevin
  • 41,694
  • 12
  • 53
  • 70
1

Avoid using explicit loops like for and while. Use array_map (it can take the variable mumber of arrays to traverse):

$result = array_map(function (...$column) {
    return implode(',', $column);
}, ...$array);

Here is the demo.

By the way, in linear algebra this is called transpose of the matrix.

From PHP7.4 and higher, the same technique can be written as: (Demo)

var_export(
    array_map(
        fn() => implode(',', func_get_args()),
        ...$array
    )
);

Or: (Demo)

var_export(
    array_map(
        fn(...$column) => implode(',', $column),
        ...$array
    )
);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
sevavietl
  • 3,762
  • 1
  • 14
  • 21
0

I assume that you have defined $column as the total number of column in your db table. Use array_column to get the value according to the column key.

$result = array();
for ($i = 0; $i < $column; $i++) {
    $res = array_column($arr, $i);
    $result[] = implode(",", $res);
}

For more information about array_column function, check this link.

Oh, just to let you know that array_column function only works for PHP 5.5 and higher.

david
  • 3,225
  • 9
  • 30
  • 43
0

check if this is what you want

$arr = array(//your array); 
$newArr = array(); //data wil lbe saved here
foreach($arr as $arr_one){
    $string = "";
    foreach($arr_one as $subArr){
        $string.=$subArr.",";
    }
    array_push($newArr,rtrim($string,','));
}

var_dump($newArr);
pavithra rox
  • 1,056
  • 3
  • 11
  • 33