0

I have an array like:

$array  = array('OPTM3000', 'All-Stud','OPTM3002','MUSC1001','PATH3000', 'OPTM3004');

I want to UNSET the values in the array that are not prefixed with OPTM.

How should that be done?

PHP 5.5

IlludiumPu36
  • 4,196
  • 10
  • 61
  • 100
  • unset or remove? Do you want to preserve in index positions (e.g. OPTM3002 stays at index 2 or does it become 1)? – Salman A Sep 30 '21 at 08:39
  • Does this answer your question? [Unset array Items matching a pattern](https://stackoverflow.com/questions/9444427/unset-array-items-matching-a-pattern) – thchp Sep 30 '21 at 08:40
  • @thchp Not really. I have posted an answer – IlludiumPu36 Sep 30 '21 at 08:53

8 Answers8

1
// Don't need this if using PHP 8
if (!function_exists('str_starts_with')) {
    function str_starts_with($haystack, $needle) {
        return (string)$needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0;
    }
}

$array = array('OPTM3000', 'All-Stud','OPTM3002','MUSC1001','PATH3000', 'OPTM3004');
for ($i = 0; $i < count($array); ++$i) {
    if (!str_starts_with($array[$i], 'OPTM'))
        unset($array[$i]);
}

// Optional to re-index array
$array = array_values($array);
Jacob Mulquin
  • 3,458
  • 1
  • 19
  • 22
1

You can use foreach/str_contains/unset like:

$array  = array('OPTM3000', 'All-Stud','OPTM3002','MUSC1001','PATH3000', 'OPTM3004');
foreach($array as $key => $value){
    if(!str_contains($value,'OPTM')){
        unset($array[$key]);
    }
}
print_r($array); 
/*
Array
(
    [0] => OPTM3000
    [2] => OPTM3002
    [5] => OPTM3004
)

*/ 

Reference:

Simone Rossaini
  • 8,115
  • 1
  • 13
  • 34
  • 1
    PS str_contains is php8+, but this will check if the prefix is *anywhere in the string*, but OP want to check if it's a *prefix*, which means you'll have to use str_starts_with – hanshenrik Sep 30 '21 at 08:48
1

Use this

$array  = array('OPTM3000', 'All-Stud','OPTM3002','MUSC1001','PATH3000', 'OPTM3004');

$new_array = array_filter($array, function($v,$k) {
    return strpos($v,'OPTM') !==false;
}, ARRAY_FILTER_USE_BOTH);
  • This unexplained answer will match any string the "contains" the needle, not only if the "starts with" the needle. This question asks to match "prefixes". – mickmackusa Nov 30 '22 at 21:02
1

try this

$array  = array('OPTM3000', 'All-Stud','OPTM3002','MUSC1001','PATH3000', 'OPTM3004');

$newArray = array();

foreach($array as $value) {
  if ( 'OPTM' == substr($value, 0, 4) ) {
     $newArray[] = $value;
  }
}
Bhanu Pratap
  • 144
  • 6
1

Use Unset or if you don like to use unset function one possible way will be to work with array_map and array_filter:

$array  = array('OPTM3000', 'All-Stud','OPTM3002','MUSC1001','PATH3000', 'OPTM3004');
$arr = array_map(function($item) {
    if ( str_contains($item,'OPTM')) {
      return $item;    
    }
}, $array);

$arr = array_filter($arr);
print_r($arr); 
Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
  • This unexplained answer will match any string the "contains" the needle, not only if the "starts with" the needle. This question asks to match "prefixes". – mickmackusa Nov 30 '22 at 21:03
1

A short approach to reach the same value

$array  = array('OPTM3000', 'All-Stud','OPTM3002','MUSC1001','PATH3000', 'OPTM3004');

$filtred = array_filter($array, function($item){
    return  (strncmp($item, 'OPTM', 4)==0);
});

var_dump($filtred);

to make your code more dynamic and optimal you can do like this

$array  = array('OPTM3000', 'All-Stud','OPTM3002','MUSC1001','PATH3000', 'OPTM3004');

$prefix = 'OPTM';
$array = array_filter($array, function($item) use($prefix){
    return  (strncmp($item, $prefix, strlen($prefix))==0);
});

var_dump($array);
Armen Grigoryan
  • 521
  • 2
  • 9
0

You can iterate the elements in your array using a regex like:

foreach($array as $element){
    $pattern = "OPTM/";
    if(echo preg_match($pattern, $element) == 0){
        $key = array_search($element, $array);
        unset($array[$key]);
    }
}
s_frix
  • 323
  • 2
  • 11
0

OK, this works for me.

$array  = array('OPTM3000', 'All-Stud','OPTM3002','MUSC1001','PATH3000', 'OPTM3004');
            
$allowed_prefix = 'OPTM';

foreach ( $array as $key => $text )  
{
    if (strpos($text, $allowed_prefix) !== 0) {
        unset($array[$key]);
    }
}   
IlludiumPu36
  • 4,196
  • 10
  • 61
  • 100