PHP在数组中查找特定的东西

时间:2012-02-21 10:47:38

标签: php arrays

这是我的数组

Array (
    [0] => "8266_hal_url"
    [1] => "8266_hal_picture"
    [2] => "8266_hal_status" 
    [3] => "8266_hal_qwert"
    [4] => "4224423_hal_status"
    [5] => "4223_hal_status"
)

如何在数组中找到所有hal_status并将其传递给查询?

3 个答案:

答案 0 :(得分:7)

PHP> = 5.3.0

$testData = array ( '8266_hal_url',
                    '8266_hal_picture',
                    '8266_hal_status',
                    '8266_hal_qwert',
                    '4224423_hal_status',
                    '4223_hal_status',
                  );
$testNeedle = 'hal_status';

$result = array_filter($testData, function($arrayEntry) use ($testNeedle) { 
  return (strpos($arrayEntry,$testNeedle) !== false); 
});

var_dump($result);

答案 1 :(得分:0)

$result=array();
foreach ($myarray as $element)
  if (strpos($element,'hal_status')===false) continue;
  else $result[]=$element;

print_r($result);

答案 2 :(得分:-1)

试试这个:

$result = array();

foreach($array as $item)
{
    if(strpos($item, 'hal_status') !== false)
    {
        $result[] = $item;
    }
}

print_r($result);