我正在尝试打印特定的数组元素

时间:2014-06-10 11:26:27

标签: php arrays

我正在尝试打印数组元素,这是内容"伟大的食物"只有字。我的数组

Array
  (
   [0] => Great food and Great Atmosphere! Will definately go back before I leave!!
   [1] => Great place
   [2] => Great place
 )

2 个答案:

答案 0 :(得分:2)

尝试使用strpos()

$arr = array('Great food and Great Atmosphere! Will definately go back before I leave!!', 'Great place', 'Great place');
$findme ='Great food';
foreach($arr as $a) {
 if(strpos($a, $findme) !== false) {
  $newarr[] = $a;
 }
}
print_r($newarr);//Great food and Great Atmosphere! Will definately go back before I leave!!

如果您只想Great food匹配更改if(strpos($a, $findme) !== false) {if($a == $findme) {

答案 1 :(得分:0)

您可以使用array_filter功能。

function match_string($element){
  return strpos($element, 'Great food');
}

$matches = array_filter($input_array,"match_string");