Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中增加字母后的数字_Php - Fatal编程技术网

如何在PHP中增加字母后的数字

如何在PHP中增加字母后的数字,php,Php,我希望在某个字母后增加一个数字 我有一个自己的id列表,我想在每次添加新id时增加它,而不必手动写入它 $ids = array('303.L1', '303.L2', '303.L3', '303.L4'); 因此,我使用END()函数从这个数组中提取最后一个id 这是我尝试过的,但我无法得到结果 $i = 0; while($i <= count($ids)){ $i++; $new_increment

我希望在某个字母后增加一个数字

我有一个自己的id列表,我想在每次添加新id时增加它,而不必手动写入它

$ids = array('303.L1', '303.L2', '303.L3', '303.L4');
因此,我使用END()函数从这个数组中提取最后一个id

这是我尝试过的,但我无法得到结果

        $i = 0;
        while($i <= count($ids)){

            $i++;
            $new_increment_id = 1;
            $final_increment = end($last_id) + $new_increment_id;

        }
        echo $final_increment;
$i=0;

while($i如果您不想要预定义的列表,但想要在$ids变量中返回定义数量的id,您可以使用以下代码

<?php

$i              = 0;
$number_of_ids = 4;
$id_prefix  = "303.L";
$ids            = array();

while($i < $number_of_ids){
    $ids[] = $id_prefix . (++$i); // adds prefix and number to array ids.
}

var_dump($ids);
// will output '303.L1', '303.L2', '303.L3', '303.L4'
?>

我有点困惑,因为你说“不用手动编写”。但我想我有一个解决办法:

$ids = array('303.L1', '303.L2', '303.L3', '303.L4');
$i = 0;
while($i <= count($ids)){

    ++$i;
    //Adding a new item to that array
    $ids[] = "303.L" . $i;
}

不能只使用数组键(+1,如果不想从0开始)?抱歉,将1声明为$i变量是一个错误,我会将其更改为0。变量之前任何应该递增的部分是否会发生变化?是否还有其他动态的部分?想法是像mysql使用ID一样使用普通递增,但我有“303.L”在递增数字前加前缀。它可以转到303.L50000、303.L50000和303.L50000++…是的,手动写入我的意思是,每次插入新id时都在输入上写入。我编辑了我的问题,并在“L”之后添加了一种新的递增最后一个数字的方法但问题是,我有一个双点。在结尾处仍然很困惑。但我相信这个答案会为您自动完成这个过程。它会自动增加id,您不必担心它。明白了,只需在preg_split(“/[0-9]+/”,end($ids))的数字后面添加点,它就会变成preg_split(“/[0-9-.]+/”,end($ids))其思想是使用数据库中存储的值作为id,ti可以是303.L,也可以是450.S前缀,我只需要在这个前缀后增加数字,但首先需要提取这个前缀。
$ids = array('303.L1', '303.L2', '303.L3', '303.L4');
$i = 0;
while($i <= count($ids)){

    ++$i;
    //Adding a new item to that array
    $ids[] = "303.L" . $i;
}
//Grab last item in array
$current_index = $ids[count($ids) - 1];
//Separates the string (i.e. '303.L1') into an array of ['303', '1']
$exploded_id = explode('.L', $current_index);
//Then we just grab the second item in the array (index 1)
$i = $exploded_id[1];