PHP:从数组创建数组
我有一个表单,它发布了4个数组,我需要将这些数组合并并最终组成电子邮件。四个阵列:PHP:从数组创建数组,php,arrays,Php,Arrays,我有一个表单,它发布了4个数组,我需要将这些数组合并并最终组成电子邮件。四个阵列: $quantityArray = $this->input->post('qty'); $dimensionArray = $this->input->post('dimension'); $thicknessArray = $this->input->post('thickness'); $descriptionArray = $this->input-&
$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,但没有得到我想要的结果。感谢您在这方面的帮助。如果这些数组的长度相同,下面是示例代码:
$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);
如果这些数组的长度相同,下面是示例代码:
$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);
如果始终存在以下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);
如果始终存在以下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);
这也可能起作用:
<?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>";
?>
这也可能起作用:
<?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>";
?>
如果我们假设这些数组的长度相同,那么下面是我的代码:
如果我们假设这些数组的长度相同,那么下面是我的代码:
一个简单的循环就可以做到这一点。顺便问一下,您希望得到什么样的数组?两个数组看起来都一样。对不起,只是复制并粘贴了示例结果。一个简单的循环就可以做到这一点。顺便问一下,您对结果数组的期望值是多少?两个数组看起来都一样。对不起,只是复制并粘贴了示例结果。非常简洁。谢谢你在这方面的帮助!非常简洁。谢谢你在这方面的帮助!