2

I use preg_match_all($pattern, $string, $matches) regularly on data, and basically, I always need to remove either £0.00 or $0.00 or €0.00 from the array should it be in there.

I never know the position within the array that it will be in, if at all.

I've tried unset($matches['£0.00']) and unset($matches[0]['£0.00']) but didn't work

Also, can it be done without using the actual currency symbol and maybe regex \p{Sc}

So, summary, I have an array of numbers with currency symbols, and need to remove any zero entries.

sample array:

Array ( [0] => Array ( [0] => £18.99 [1] => £0.00 [2] => £0.00 [3] => £0.00 ) ) 

Is this possible?

4

2 回答 2

6

preg_match()和的用法preg_match_all()有时会很奇怪

$matches将是模式匹配的数组 - 在这种情况下,如果你

preg_match("/\p{Sc}0\.00/","£0.00",$matches);
//$matches[0] => £0.00

或者,如果您()在模式中添加括号,您可以提取一部分

preg_match("/\p{Sc}(0\.00)/","£0.00",$matches);
//$matches[0] => £0.00
//$matches[1] => 0.00

所以要解决这个问题试试这个

foreach($sample_array as $key => $value)
{
     if(preg_match("/(\$|\p{Sc})0\.00/",$value))
     {
          unset($sample_array[$key]);
     }
}

|请注意表示$或的管道£- 如果为欧元符号添加另一个管道,它也会捕获它 - 还要注意我们unset()在原始数组中 -ing 一个键而不是unset()-ing$matches数组

于 2012-11-19T06:31:21.857 回答
0
function remove_element($arr, $val)

        {

            foreach ($arr as $key => $value)

            {

                if ($arr[$key] == $val)

                {

                unset($arr[$key]);

                }

            }

            return $arr = array_values($arr);

        }

您可以使用此函数,这些函数在删除指定值后返回数组元素。

于 2012-11-19T07:05:47.277 回答