如何自动计数数字和每行自动添加逗号(从我想要的数字开始)

时间:2016-04-01 09:06:08

标签: php

实施例: 我有16行:

Row-1
Row-2
Row-3
Row-4
...
Row-16

我在表单中放入16行并输入起始编号(如17)后,我希望它会自动添加逗号并从第17位开始计数到 ​​- > 32

结果将是

17,Row-1
18,Row-2
19,Row-3
20,Row-4
...
32,Row-16

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

首先,您需要格式化16行:第1行,第2行......第16行。

function addPrefix(&$value,$key) {
    $value = "Row-".$value;
}

$row    =   range(1,16);

array_walk($row, 'addPrefix');     // Function to create an array of elements Row-1, Row-2, Row-3, Row-4...Row-16

/* You may ignore the above functions in case you already have a previously set up array */

$count = 17;

foreach($row as $r) {

    $result[]   =   $count.",".$r;
    $count++;
}

echo "<pre>";
print_r($result);
echo "</pre>";

希望这有帮助。