0

I have an array that contains data, so it'll be like:

array(27.42, 27.71, 26.61, 204.12 1995-1-17)

And I want to remove all words with dashes so that I'm left with an array that looks like:

array(27.42, 27.71, 26.61, 204.12)

Here's a picture of the exact array.

https://i.stack.imgur.com/KRPDt.png

Do I use regex? What is the best method. I tried using an if function to replace all words that include "-" with " ", but it didn't work.

Any help is much appreciated!

2 Answers2

0
foreach ($array as $index=>$value)
{
 if (strstr($value,'-')) 
 {
  $ex=explode(" ",$value,2);
  $array[$index]=@$ex[0];
 }
}

Simply walk through the array and check if the value contains dash ... Please note the foreach is associative, so you will get the index of the value inside the array, this is why you can unset it.

Gipsz Jakab
  • 433
  • 3
  • 9
0

My try :

<?php
$value = array(27.42, 27.71, 26.61, '204.12 1995-1-17');

$pattern = '/\s*\b\w*\-\w*\b/';

$result = preg_replace($pattern, '', $value);

var_dump($result);
jmleroux
  • 947
  • 6
  • 17