Maddy
Maddy

Reputation: 53

split first part of string in every index of array and retain the rest of the string

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

Answers (5)

Don't Panic
Don't Panic

Reputation: 41810

One more option:

foreach ($array as $value) {
    $new[] = explode(' ', $value, 2)[1];
}

Dereferencing the explode result requires PHP >= 5.4.

Upvotes: 0

RomanPerekhrest
RomanPerekhrest

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

AbraCadaver
AbraCadaver

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

ScaisEdge
ScaisEdge

Reputation: 133360

for($i = 0; $i < count($array); $i++) {
   $result[$i] =substr($array[$i], 11,);  
}

Upvotes: 2

Eihwaz
Eihwaz

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

Related Questions