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
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);
merge multiple arrays with their same key value 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 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
Create new Array under same key from multiple Array in PHP
A simple foreach loop will suffice
$a = array("Algebra", "Arithmetic");
$b = array("08/01/2020", "08/02/2019");
$c = array("08/01/2020", "08/02/2019");
foreach( $a as $i=>$v ) {
$new[] = sprintf( '%s,%s,%s', $v, $b[$i], $c[$i] );
}
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
assoc arrays to two different arrays and join them
For PHP version > 5.5. You can simply use array_column
along with implode
function like as
echo $first = implode(',',array_column($your_array,'one'));
echo $second = implode(',',array_column($your_array,'two'));
For lower version you can use array_map
like as
echo $first = implode(',',array_map(function($v){ return $v['one'];} ,$your_array));
echo $second = implode(',',array_map(function($v){ return $v['two'];},$your_array));
How to create a string out of elements in a 2D- array in php?
You need to use implode
twice as along with array_map
$array = Array(Array(1,1,0,1),Array(1,0,0,0),Array(1,0,0,0));
$result = array_map('implode',$array);
echo implode($result);//11011000100010
Using foreach
as
foreach($array as $key => $value){
foreach($value as $v){
echo $v;
}
}
Related Topics
Initialize Class Property with an Anonymous Function
PHP Pdo Prepare Repetitive Variables
When Using Gmail for Smtp, Can You Set a Different "From" Address
Access Query String Values from Laravel
How to Check If String Is a Valid Xml Element Name
Replace Multiple Newlines, Tabs, and Spaces
Special Characters in PHP/Mysql
Utf-8 Safe Equivalent of Ord or Charcodeat() in PHP
PHP - Split String in Key/Value Pairs
How to Generate Random 64-Bit Value as Decimal String in PHP
Single Quotes or Double Quotes for Variable Concatenation
How to Schedule the Execution of a PHP Script on the Server Side