Home > Software design >  PHP - UNSET values in array not containing prefix
PHP - UNSET values in array not containing prefix

Time:09-30

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

CodePudding user response:

// 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);

CodePudding user response:

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:

CodePudding user response:

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);

CodePudding user response:

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]);
    }
}

CodePudding user response:

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;
  }
}

CodePudding user response:

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); 

CodePudding user response:

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]);
    }
}   

CodePudding user response:

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);
  •  Tags:  
  • php
  • Related