0

我有一个表单,它发布了 4 个数组,我需要将它们组合起来并最终写入电子邮件。四个数组:

$quantityArray    = $this->input->post('qty');
$dimensionArray   = $this->input->post('dimension');
$thicknessArray   = $this->input->post('thickness');
$descriptionArray = $this->input->post('description');

都将具有相同的数组长度,并且每个索引都是相关的。如何组合 4 个数组,例如

[0]
     'qty' => '1',
     'dimenesion => '2x2',
     'thickness' => '2in',
     'description' => 'this is the description'
[1]
     'qty' => '1',
     'dimenesion => '2x2',
     'thickness' => '2in',
     'description' => 'this is the description'

我已经尝试了 array_combined、array_merged 并且无法获得我正在寻找的结果。感谢您对此的帮助。

4

4 回答 4

2

如果这些数组的长度相同,这里是示例代码:

$resultArray = array();
foreach($quantityArray as $index => $qty) {
    $resultArray[$index]['qty'] = $qty;
    $resultArray[$index]['dimenesion'] = $dimensionArray[$index];
    $resultArray[$index]['thickness'] = $thicknessArray[$index];
    $resultArray[$index]['description'] = $descriptionArray [$index];
}

print_r($resultArray);
于 2013-10-25T18:28:42.520 回答
1

这也可能有效:

<?php
//...
$quantityArray    = $this->input->post('qty');
$dimensionArray   = $this->input->post('dimension');
$thicknessArray   = $this->input->post('thickness');
$descriptionArray = $this->input->post('description');

//
// combine them:
//
$combined = array();
$n = count($quantityArray);
for($i = 0; $i < $n; $i++)
{
  $combined[] = array(
    'qty' => $quantityArray[$i],
    'dimenesion' => $dimensionArray[$i],
    'thickness' => $thicknessArray[$i],
    'description' => $descriptionArray[$i]
  );
}
//
echo "<pre>";
print_r($combined);
echo "</pre>";
?>
于 2013-10-25T18:33:29.400 回答
1

如果我们假设这些数组的长度相同,这是我的代码:

$quantityArray = array(1, 1, 5, 3);
$dimensionArray = array("2x2", "3x3", "4x4", "2x2");
$thicknessArray = array("2in", "3in", "4in", "2in");
$descriptionArray = array("this is the description 1", "this is the description 2 ", "this is the description3 ", "this is the description4" );

$myCombinArray = array();
foreach ( $quantityArray as $idx => $val ) {
    $subArray = array (
            'qty' => $quantityArray [$idx],
            'dimenesion' => $dimensionArray [$idx],
            'thickness' => $thicknessArray [$idx],
            'description' => $descriptionArray [$idx] 
    );
    array_push ( $myCombinArray, $subArray );
}
print_r($myCombinArray);
于 2013-10-25T18:56:31.603 回答
0

如果总是有这 4 个数组,那么简单的方法如何:

$quantityArray    = $this->input->post('qty');
$dimensionArray   = $this->input->post('dimension');
$thicknessArray   = $this->input->post('thickness');
$descriptionArray = $this->input->post('description');

$combinedArray = [$quantityArray, $dimensionArray, $thicknessArray, $descriptionArray];

# old syntax:
# $combinedArray = array($quantityArray, $dimensionArray, $thicknessArray, $descriptionArray);
于 2013-10-25T18:28:52.950 回答