2

I have the following Array:

Array
(
    [0] => text
    [1] => texture
    [2] => beans
    [3] => 
)

I am wanting to get rid of entries that don't contain a-z or a-z with a dash. In this case array item 3 (contains just a space).

How would I do this?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
ritch
  • 1,760
  • 14
  • 37
  • 65
  • And how do you mean get rid off, if you have [0 1 2 3] and 2 is spaces only do you want [0 1 2] or [0 1 3]? – Yet Another Geek Jun 13 '11 at 13:05
  • I want to delete item 3. – ritch Jun 13 '11 at 13:06
  • 1
    The reason that Yet Another Geek asked that question (and provided two, specific options) is that "I want to delete item 3" is too vague. Repeating it is not helpful. Please answer with one of the two specific options that Yet Another Geek carefully constructed for you. – Lightness Races in Orbit Jun 13 '11 at 13:09

4 Answers4

5

Try with:

$input = array( /* your data */ );

function azFilter($var){
    return preg_match('/^[a-z-]+$/i', $var);
}
$output = array_filter($input, 'azFilter');

Also in PHP 5.3 there is possible to simplify it:

$input = array( /* your data */ );

$output = array_filter($input, function($var){
    return preg_match('/^[a-z-]+$/i', $var);
});
hsz
  • 148,279
  • 62
  • 259
  • 315
1

For the data you have provided in your question, use the array_filter() function with an empty callback parameter. This will filter out all empty elements.

$array = array( ... );
$array = array_filter($array);

If you need to filter the elements you described in your question text, then you need to add a callback function that will return true (valid) or false (invalid) depending on what you need. You might find the ctype_alpha functions useful for that.

$array = array( ... );
$array = array_filter($array, 'ctype_alpha');

If you need to allow dashes as well, you need to provide an own function as callback:

$array = array( ... );
$array = array_filter($array, function($test) {return preg_match('(^[a-zA-Z-]+$)', $test);});

This sample callback function is making use of the preg_match() function using a regular expression. Regular expressions can be formulated to represent a specifc group of characters, like here a-z, A-Z and the dash - (minus sign) in the example.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • `ctype_alpha` does not contain dash character. – hsz Jun 13 '11 at 13:09
  • Will this work if the array item contains e.g. 2 blank spaces? – ritch Jun 13 '11 at 13:09
  • @hsz: edited. @ritch: What do you mean by work? Should two blank spaces count as valid or invalid? Are you actually able to read PHP? If two spaces should be removed, then the answer is: Yes. – hakre Jun 13 '11 at 13:12
  • @ritch: Yes that would count as invalid. Two spaces do not match a-z or A-Z or the dash. – hakre Jun 13 '11 at 13:17
1

Try:

<?php
    $arr = array(
        'abc',
        'testing-string',
        'testing another',
        'sdas 213',
        '2323'
    );

    $tmpArr = array();
    foreach($arr as $str){
        if(preg_match("/^([-a-z])+$/i", $str)){
            $tmpArr[] = $str;
        }
    }
    $arr = $tmpArr;
?>

Output:

array
  0 => string 'abc' (length=3)
  1 => string 'testing-string' (length=14)
Shef
  • 44,808
  • 15
  • 79
  • 90
0

Ok , simply you can loop trough the array. Create an regular expression to test if it matches your criteria.If it fails use unset() to remove the selected element.

Gayan Hewa
  • 2,277
  • 4
  • 22
  • 41