仅显示不包含的数组值

时间:2013-11-13 20:47:26

标签: php regex arrays

foreach ($prefs as $who => $pref) {
 if($who != 'public'){
  $team_users .=$who.',';
  }
 }       
echo $team_users;

我只想显示数组中不包含文字' public'在某些情况下,它可能是publicXX或public1234等。

7 个答案:

答案 0 :(得分:2)

您可以使用array_filter()进行回调来实现此目的:

$result = array_filter($prefs, function($item) {
    return (strpos($item, 'public') === FALSE);
});
echo implode(',', $result);

答案 1 :(得分:1)

http://us3.php.net/strpos

if (strpos($who, 'public') === false) {
  //some code
} else {
  //some code
}

答案 2 :(得分:0)

  

I'd like to only display data from the array that does not contain the text 'public' in some cases it could be publicXX or public1234, etc.

变化:

if($who != 'public'){...}

到此:

if(strpos($who, 'public') !== false){...}

答案 3 :(得分:0)

foreach ($prefs as $who => $pref) {
    if(strpos($who, 'public') !== false){
        $team_users .=$who.',';
    }
}       
echo $team_users;

答案 4 :(得分:0)

您可以尝试这样的事情:

foreach ($prefs as $who => $pref) {
   if(!preg_match("/public/i", $who)){
       $team_users .= $who.',';
   }
} 

答案 5 :(得分:0)

<?php

$a = array('public1', 'asdqwe', 'as33publics', 'helloworld');

$text = implode(', ', array_filter($a, function($v) {
    return strstr($v, 'public') === false;
}));

echo $text;

答案 6 :(得分:0)

我会用正则表达式来做。

$pattern = '/public/'; 

if(preg_match($pattern,$who)){
//skip to the next entry
}