在数组中查找单词并使用php存储到新数组中?

时间:2013-08-13 14:46:36

标签: php arrays sorting multidimensional-array find

我有一个类似

的数组
Array
(
   [0] => Canon ProII PE-1000GC (MBL), Metallic Blue Electric Guitar
   [1] => Captain Tsubasa DVD Box
   [2] => Canon EOS Kiss X4 (550D / Rebel T2i) + Double Zoom Lens Kit
   [3] => Fresh Chiba Roasted Seaweed
)

如果我试图找到canon,我的预期结果将在下面给出

Array
(
   [0] => Canon ProII PE-1000GC (MBL), Metallic Blue Electric Guitar      
   [1] => Canon EOS Kiss X4 (550D / Rebel T2i) + Double Zoom Lens Kit      
)

如何找到并将此数组存储到其他变量中。请指导我。

1 个答案:

答案 0 :(得分:0)

循环显示值并使用strpos()搜索单词Canon的值。

foreach ($data as $value) {
   if (strpos($value, 'Canon') !== false) { //if string contains 'Canon'
    $newArray[] = $value; //store it into a new array
   }
}
print_r($newArray);

输出:

Array
(
    [0] => Canon ProII PE-1000GC (MBL), Metallic Blue Electric Guitar
    [1] => Canon EOS Kiss X4 (550D / Rebel T2i) + Double Zoom Lens Kit
)

Working demo!