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.
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];
}
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 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
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 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.
How to combine and transpose 2 PHP arrays
If the values in $types
are unique, you can do this:
$combined = array_combine($types, $numbers);
This will yield:
[
'a' => 1,
'b' => 2,
'c' => 3,
]
Which you can then iterate over with:
foreach ($combined as $type => $number)
Or just:
foreach (array_combine($types, $numbers) as $type => $number)
If the values in $types
are not unique, you can do this:
$combined = array_map(function($a, $b) { return [$a, $b]; }, $types, $numbers);
Related Topics
Printing a New Line in a CSV File Cell
Why Are $_Server["Php_Auth_User"] and $_Server["Php_Auth_Pw"] Not Set
Passing JavaScript Variables to PHP
How to Retrieve Youtube Video Details from Video Url Using PHP
How Add Custom Validation Rules When Using Form Request Validation in Laravel 5
Using Comparison Operators in a PHP 'Switch' Statement
Laravel 5.2 - Pluck() Method Returns Array
What Do Two Colons Mean in PHP
How to Make Laravel Eloquent "In" Query
Curl_Exec Printing Results When I Don't Want To
Wordwrap/Cut Text in HTML String
Ajax/PHP - How to Get Posted Data to Load into a Modal on the Same Page
Tracking Email with PHP and Image
Storage in Laravel Says Symlink - No Such File
How to Get PHPunit Mockobjects to Return Different Values Based on a Parameter