Reputation: 53
Below is my array:
Array
(
[0] => 2016-03-31 abc xyz
[1] => 2016-03-31 ef yz
[2] => 2016-04-05 ghij aei
)
I need to remove the date from all the indexes in the above array and retain only the rest of the string.
Expected output:
Array
(
[0] => abc xyz
[1] => ef yz
[2] => ghij aei
)
Thanks, Madhuri
Upvotes: 0
Views: 58
Reputation: 41810
One more option:
foreach ($array as $value) {
$new[] = explode(' ', $value, 2)[1];
}
Dereferencing the explode
result requires PHP >= 5.4.
Upvotes: 0
Reputation: 92854
Considering the first space in the string as delimiter:
foreach ($arr as &$v) {
$v = substr($v, strpos($v, " "));
}
print_r($arr);
The output:
Array
(
[0] => abc xyz
[1] => ef yz
[2] => ghij aei
)
Upvotes: 0
Reputation: 78994
Since there are other answers:
$result = preg_replace('/\d\d\d\d-\d\d-\d\d(.*)/', '$1', $array);
Or it could be shorter:
$result = preg_replace('[\d-]{10}(.*)/', '$1', $array);
Upvotes: 0
Reputation: 133360
for($i = 0; $i < count($array); $i++) {
$result[$i] =substr($array[$i], 11,);
}
Upvotes: 2
Reputation: 1234
Something like this will do, if the dates always come first and are in the same format (10 symbols):
foreach ($array as &$v) {$v = trim(substr($v, 10));}
Upvotes: 3