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

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

4

7 回答 7

2

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

$result = array_filter($prefs, function($item) {
    return (strpos($item, 'public') === FALSE);
});
echo implode(',', $result);
于 2013-11-13T20:51:57.783 回答
1

http://us3.php.net/strpos

if (strpos($who, 'public') === false) {
  //some code
} else {
  //some code
}
于 2013-11-13T20:53:05.573 回答
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){...}
于 2013-11-13T20:51:37.117 回答
0
foreach ($prefs as $who => $pref) {
    if(strpos($who, 'public') !== false){
        $team_users .=$who.',';
    }
}       
echo $team_users;
于 2013-11-13T20:52:18.837 回答
0

你可以尝试这样的事情:

foreach ($prefs as $who => $pref) {
   if(!preg_match("/public/i", $who)){
       $team_users .= $who.',';
   }
} 
于 2013-11-13T20:53:34.020 回答
0
<?php

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

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

echo $text;
于 2013-11-13T20:54:23.887 回答
0

我会用正则表达式来做。

$pattern = '/public/'; 

if(preg_match($pattern,$who)){
//skip to the next entry
}
于 2013-11-13T20:55:11.037 回答