PHP 自增数组
PHP Autoincrement Array
我在扩展 PHP 数组时遇到问题。这是我的代码示例:
<select name="PaymentCounts">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<?php
$arr = array ('Client' => "Alex", 'BillNumber' => "123", 'PaymentCounts' => "1", 'Amounts' =>
array ('PaymentNumber' =>
array( array('Amount1' => "100.00", 'AmountDate1' => "27.05.2015"))), 'ActiveClient => "1");
echo "<pre>";
print_r($arr);
echo "</pre>";
?>
这就是 PaymentsCount 为 1 时的样子。如果 Payments 计数大于 1,有人能给我一个方法吗?我的 PHP 数组可以自动扩展,更具体地说,开始复制这个
array('Amount1' => "100.00", 'AmountDate1' => "27.05.2015"),
array('Amount2' => "100.00", 'AmountDate2' => "27.06.2015")
我觉得if,for和push_array一定要合并,但是我自己解决不了。如果您还给我建议如何使 Amount$i 和 AmountDate$i 索引递增 1 直到达到 PaymentsCount
,我将非常高兴
您可以考虑使用 for()
和 array_push()
函数。
原理图代码可能如下所示:
$paymentsArray = array();
$day = "27";
$month = "05";
$year ="2015";
for($i=0; $i <= $PaymentCounts; $i++)
{
array_push($paymentsArray, array('Amount1' => "100.00", 'AmountDate1' => "$day.$month.$year"));
$month++;
}
$arr = array ('Client' => "Alex", 'BillNumber' => "123", 'PaymentCounts' => "1", 'Amounts' => $paymentsArray);
我在扩展 PHP 数组时遇到问题。这是我的代码示例:
<select name="PaymentCounts">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<?php
$arr = array ('Client' => "Alex", 'BillNumber' => "123", 'PaymentCounts' => "1", 'Amounts' =>
array ('PaymentNumber' =>
array( array('Amount1' => "100.00", 'AmountDate1' => "27.05.2015"))), 'ActiveClient => "1");
echo "<pre>";
print_r($arr);
echo "</pre>";
?>
这就是 PaymentsCount 为 1 时的样子。如果 Payments 计数大于 1,有人能给我一个方法吗?我的 PHP 数组可以自动扩展,更具体地说,开始复制这个
array('Amount1' => "100.00", 'AmountDate1' => "27.05.2015"),
array('Amount2' => "100.00", 'AmountDate2' => "27.06.2015")
我觉得if,for和push_array一定要合并,但是我自己解决不了。如果您还给我建议如何使 Amount$i 和 AmountDate$i 索引递增 1 直到达到 PaymentsCount
,我将非常高兴您可以考虑使用 for()
和 array_push()
函数。
原理图代码可能如下所示:
$paymentsArray = array();
$day = "27";
$month = "05";
$year ="2015";
for($i=0; $i <= $PaymentCounts; $i++)
{
array_push($paymentsArray, array('Amount1' => "100.00", 'AmountDate1' => "$day.$month.$year"));
$month++;
}
$arr = array ('Client' => "Alex", 'BillNumber' => "123", 'PaymentCounts' => "1", 'Amounts' => $paymentsArray);