Transpose Multidimensional Array and Join Values with Commas

Transpose multidimensional array and join values with commas

Just extract each column incrementally and join it. This only works with sub-arrays as shown with 0 based sequential indexes:

$i = 0;
while($new = array_column($array, $i)) {
$result[] = implode(', ', $new);
$i++;
}

For other non-0 based, non-sequential, non-integer, just re-index the sub-arrays before extracting and joining:

$array = array_map(function($v) { return array_values($v); }, $array);

Transpose 2d array, join second level with commas, and join first level with pipes

I'm assuming that you have this array:

$array = array (
array ('01','03','02','15'),
array ('05','04','06','10'),
array ('07','09','08','11'),
array ('12','14','13','16')
);

In which case, you can do this:

$tmpArr = array();
foreach ($array as $sub) {
$tmpArr[] = implode(',', $sub);
}
$result = implode('|', $tmpArr);
echo $result;

See it working

How to transpose and implode subarray data within a multidimensional array to create a flat output array?

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

Merge Arrays But Join Corresponding Keys

You can pass both the arrays to array_map to pivot them into the format you want, then implode the inner arrays so you end up with an array of strings rather than an array of arrays.

$result = array_map(
function(...$row) { return implode(' ', $row); },
$col1Array, $col2Array
);

how to merging multiple key and values in PHP

Direct Output

If you just need to output your array directly to the page, you could do something like this:

foreach($original_array['category_id'] as $key => $categoryid) {
$product = $original_array['product'][$key];
$category = $original_array['category'][$key];

echo "{$categoryid} , {$product} , {$category}\n";
}

This is pretty simple and only requires a single foreach loop. This is useful if you do not need to use this data multiple times in different places.



Formatted Array Output

If you may need to use this later in code, I would create a new array with the data in the loop above.
This is probably overkill if you don't ever need to access this data again.

$output = [];
foreach($original_array['category_id'] as $key => $categoryid) {
$product = $original_array['product'][$key];
$category = $original_array['category'][$key];

$output[] = ['category_id' => $categoryid, 'product' => $product, 'category' => $category];
}

This would make $output be something like

Array
(
[0] => Array
(
[category_id] => 1
[product] => Apple
[category] => Fruit
)

[1] => Array
(
[category_id] => 2
[product] => Cow
[category] => Meat
)

)

Which, of course, is trivial to output in your required format.

foreach($output as $values) {
echo "{$values['categoryid']} , {$values['product']} , {$values['category']}";
}

How to concatenate the first row of a sheet into a list separated by commas?

You can't join a Range, you join a 1-dimensional array. What's implicit here, is a call to Range.Value, which, because the range contains multiple cells, is going to be a 2D variant array: you need to pass it to Application.Transpose once to turn it into a one-dimensional array, and once more to make it vertical so that the VBA.Strings.Join function will be able to work with.

Also, watch out for implicit ActiveSheet references: unless that code is written in a worksheet module (where the implicit qualifier would be Me.), all these unqualified Range and Cells member calls are implicitly working off whatever the ActiveSheet is.

With ActiveSheet
lc = .Cells(1, .Columns.Count).End(xlToLeft).column
Dim headerCells As Variant
headerCells = .Range(.Cells(1, 1), .Cells(1, lc)).Value '2D variant array
End With

Dim headers As String
headers = Join(Application.Transpose(Application.Transpose(headerCells)), ",")

Debug.Print headers

Implode columnar values between two arrays into a flat array of concatenated strings

// updated version
$a = ['a', 'b', 'c'];
$b = ['d', 'e', 'f', 'g'];
print_r(array_map('implode', array_map(null, $a, $b)));

php concat array values into new array php

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

How convert a column from a multi-dimensional array into a comma-separated string?

First of all, you're trying to implode the strings, not explode. Secondly, no, there's no syntax shortcut for expressing the operation "join all id keys in all sub arrays together". You can do it very concisely like this though:

echo join(',', array_map(function ($i) { return $i['id']; }, $arr));

Transform an Excel matrix into values separated by commas

try:

=ARRAYFORMULA({A2:A, 
REGEXREPLACE(TRIM(TRANSPOSE(QUERY(TRANSPOSE(
IF(B2:E=1, TO_TEXT(B1:E1)&",", )),,9^9))), ",$", )})

Sample Image



Related Topics



Leave a reply



Submit