I have five different arrays in my program. All of the arrays are of the same length. For this example let's say all of the arrays contain 6 items. The first item array1[0] should be paired with alle the values of the other arrays index 0. So I get an array of all the index 0s, an array with all the index 1 and 2,3,4 and index 5...
How would one do this?
Added more details: I have the following arrays containing information of the items in a shopping_cart.
$nameArray - contains the names of the products in the basket
$productIdArray - contains the id_numbers of the products in the basket
$priceArray - array of the prices for each item in the basket
$quantityArray - array which holds the quantity of each item in the basket
etc. etc.
I'll like to change the output, so I can send a multidimensionel array containing arrays representing a single product each with all of it's values for sending it of in an ajax call...
Hope it makes sense. :)
I went with only four arrays as it should be more than enough to explain the process. There may be a more elegant solution here but it would require some more thought.
The main point is I find it easiest to think about problems like this as tables. Your instance is actually relatively simple. You have arrays of rows and you want to convert them to arrays of columns. Check out my solution.
<?php
$one = array('brown', 'green', 'red', 'yellow', 'orange', 'purple');
$two = array('cupcake', 'honeycomb', 'icecream', 'chocolate', 'jellybean', 'milkshake');
$three = array('monday', 'tuesday', 'wednesday', 'thrusday', 'friday', 'saturday');
$four = array('january', 'february', 'march', 'april', 'august', 'september');
//put all of your arrays into one array for easier management
$master_horizontal = array($one, $two, $three, $four);
$master_vertical = array();
foreach ($master_horizontal as $row) {
foreach ($row as $key => $cell) {
$master_vertical[$key][] = $cell;
}
}
echo "<PRE>";
print_r($master_vertical);
Returns...
Array
(
[0] => Array
(
[0] => brown
[1] => cupcake
[2] => monday
[3] => january
)
[1] => Array
(
[0] => green
[1] => honeycomb
[2] => tuesday
[3] => february
)
[2] => Array
(
[0] => red
[1] => icecream
[2] => wednesday
[3] => march
)
[3] => Array
(
[0] => yellow
[1] => chocolate
[2] => thrusday
[3] => april
)
[4] => Array
(
[0] => orange
[1] => jellybean
[2] => friday
[3] => august
)
[5] => Array
(
[0] => purple
[1] => milkshake
[2] => saturday
[3] => september
)
)
Since you have not posted any code you have written as of now I will give a general explanation. This looks more like a homework problem so I will refrain from posting the working solution.
let there be N arrays with variable number of elements in it.
Let Answer_Array be an array of arrays.
loop i=0 to N
tmpArray = Arrays[i]
loop j=0 to length(N)-1
add tmpArray[j] to Answer_Array[j]
end loop
end loop
If you combine the original input to an array of arrays and the final output you store it in an array of arrays, this is trivial is php.