Transpose rows and columns in a 2D array
This should give you what you need.
function transpose($array_one) {
$array_two = [];
foreach ($array_one as $key => $item) {
foreach ($item as $subkey => $subitem) {
$array_two[$subkey][$key] = $subitem;
}
}
return $array_two;
}
Then just pipe your existing array into the function and render the resulting array.
Transpose a PHP multidimensional array with predefined keys
The following function will do the job:
function transpose($data)
{
$result = [];
$keys = array_keys($data);
for ($row = 0, $rows = count(reset($data)); $row < $rows; $row++) {
foreach ($keys as $key) {
$result[$row][$key] = $data[$key][$row];
}
}
return $result;
}
Notice that the function is a general solution it doesn’t depend on the name of the keys nor on the number of entries of each key.
How to transpose array elements?
Here's an approach:
$categories = array(
'item_name' => array('I-Phone', 'samsung', 'nokia','htc'),
'item_price' => array('30.00', '20', '10', '15')
);
$out = array();
foreach($categories as $key => $a){
foreach($a as $k => $v){
$out[$k][$key] = $v;
}
}
echo '<pre>';
print_r($out);
echo '</pre>';
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);
Is there better way to transpose a PHP 2D array?
Nope. That's the most elegant.
Transpose a multidimensional associative array into a multidimensional indexed array sorted against and an external associative key
Probably the simplest thing is to just iterate over all the values, sorting them into a car indexed array. You can then use ksort
to sort the data:
$output = array();
foreach ($array as $key => $a) {
foreach ($a as $car => $v) {
$output[$car][$key] = $v;
}
}
ksort($output);
$array_cars = array_keys($output);
$compiled_data = array_values($output);
var_export($array_cars);
var_export($compiled_data);
Output:
array (
0 => 'BMW',
1 => 'Ford',
2 => 'Land Rover',
3 => 'Nissan',
4 => 'Saab',
5 => 'Volvo',
)
array (
0 =>
array (
0 => 13,
2 => 9,
),
1 =>
array (
2 => 17,
),
2 =>
array (
0 => 11,
2 => 22,
),
3 =>
array (
1 => 10,
2 => 2,
),
4 =>
array (
0 => 5,
1 => 4,
),
5 =>
array (
0 => 22,
),
)
Demo on 3v4l.org
Create a multidimensional array based on number of values
You just need to loop over the input array and build the new array
$new = [];
foreach ($in as $key=>$arr) {
$new[0][$key] = $arr[0];
$new[1][$key] = $arr[1];
}
Related Topics
I Have Three Columns, I Want to Combine Them All in One Main_Table
Add Space After Every 4Th Character
Laravel: Products,Categories and Subcategories! (Relation Ships)
Remove License Check from Script Code in Codeigniter
Checking Dates If Between Range of Dates in Laravel 5
Laravel Eloquent How to Get the Minimum Value That Is Not Null
How to Pass an Array Within a Query String
Loop Row in Bootstrap Every 3 Columns
Laravel Unique Validation on Multiple Columns
Telegram Bot Api: How to Send a Photo Using PHP
Multiple Column With Same Search String in Laravel
How to Remove Duplicate Values from a Multi-Dimensional Array in PHP
How to Use Return Inside a Recursive Function in PHP
Warning: Preg_Replace(): Unknown Modifier
PHP Multidimensional Array Search by Value
Pass a PHP String to a JavaScript Variable (And Escape Newlines)