将五个数组合并为五个“;“合并”;PHP中的数组
我的程序中有五个不同的数组。所有数组的长度都相同。在本例中,假设所有数组都包含6项。第一项array1[0]应与其他数组索引0的所有值配对。所以我得到一个包含所有索引0的数组,一个包含所有索引1和2,3,4和索引5的数组 你会怎么做 添加了更多详细信息: 我有以下数组,其中包含购物车中物品的信息将五个数组合并为五个“;“合并”;PHP中的数组,php,Php,我的程序中有五个不同的数组。所有数组的长度都相同。在本例中,假设所有数组都包含6项。第一项array1[0]应与其他数组索引0的所有值配对。所以我得到一个包含所有索引0的数组,一个包含所有索引1和2,3,4和索引5的数组 你会怎么做 添加了更多详细信息: 我有以下数组,其中包含购物车中物品的信息 $nameArray - contains the names of the products in the basket $productIdArray - contains the id_numbe
$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
等等等等
我想更改输出,这样我就可以发送一个多维数组,其中包含表示单个产品的数组,每个数组都有它的所有值,以便在ajax调用中发送它
希望它有意义。:) 我只使用了四个数组,因为它应该足以解释这个过程。这里可能有一个更优雅的解决方案,但它需要更多的思考 主要的一点是,我发现把这样的问题当作表格来考虑是最容易的。您的实例实际上相对简单。您有行数组,并且希望将它们转换为列数组。看看我的解决方案
<?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);
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
由于你还没有发布任何代码,你已经写了到现在为止,我会给一个一般性的解释。这看起来更像是一个家庭作业问题,所以我不会发布工作解决方案
<?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);
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
如果将原始输入合并到一个数组数组中,并将最终输出存储到一个数组中,那么php就很简单。事实如何:您认为如何,您尝试了什么?即使元素数量可变,也可以从我的答案中寻找一般解决方案。这不是家庭作业。这是我正在开发的一个网络商店系统。