doyz51819 2019-02-25 19:57
浏览 192
已采纳

PHP:如何从数组中的每个字符串中删除前两个字符?

Have:

[0] => 0-3019
[1] => 0-3020
[2] => 0-1031
[3] => 0-3021
[4] => 0-1004
[5] => 0-3011

Want:

[0] => 3019
[1] => 3020
[2] => 1031
[3] => 3021
[4] => 1004
[5] => 3011

Not sure how to get rid of it, tried this.

Edit: Here is the code I've tried:

$files = array_keys($_FILES);

foreach ($files AS $f) {
    $f = substr($f,2);
}

For some reason it works inside the forloop but isn't actually saving each element as the substring version of itself :(

  • 写回答

2条回答 默认 最新

  • douzhuo1853 2019-02-25 20:26
    关注

    You can reference & each exposed element in order to change the original:

    foreach ($files as &$f) {
        $f = substr($f, 2);
    }
    

    Or modify the original array using the key:

    foreach ($files as $k => $f) {
        $files[$k] = substr($f, 2);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?