Reputation: 13555
I have a Multi dimension array and what i 'd like to do is put array to insert the element in each column
for example
Multi dimension array :
Tony 14
Peter 20
I would like to insert them into a different array , so that
column0[]={Tony, Peter}
column1[]={14, 20}
Since i do not know the actual no of column , how can i achieve this?
for ($row = 1; $row <= $highestRow; $row++) {
for ($y = 0; $y < $highestColumn; $y++) {
................what should be added here................
}
}
Thank you
Upvotes: 0
Views: 117
Reputation: 6752
Check out the code below. All you're doing in your actual loop, is swapping the $y and the $row
<?php
$original_array = array(
array('Tony', 14),
array('Peter', 20)
);
print_r($original_array);
// Array
// (
// [0] => Array
// (
// [0] => Tony
// [1] => 14
// )
// [1] => Array
// (
// [0] => Peter
// [1] => 20
// )
// )
$new_array = array();
for ($row = 0; $row < count($original_array); $row++) {
for ($y = 0; $y < count($original_array[0]); $y++) {
$new_array[$y][$row] = $original_array[$row][$y];
}
}
print_r($new_array);
// Array
// (
// [0] => Array
// (
// [0] => Tony
// [1] => Peter
// )
// [1] => Array
// (
// [0] => 14
// [1] => 20
// )
// )
Upvotes: 1