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 :(
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);
}
Let's say the name of your array is $elements
, you can loop through each element of the array and use the PHP substr
to replace the current value with a portion of the string.
What substr($string, $start, $length)
does is take a part of the string $string
with a length of $length
starting from $start
.
<?php
foreach ($elements as $index=>$element) {
$elements[$index] = substr($element, 2);
}
?>
UPDATE
If it's the index that needs changind, you can do the following:
<?php
foreach ($elements as $index=>$element) {
$new_index = substr($index, 2);
$elements[$new_index] = $element;
unset($elements[$index]);
}
?>