preg_match_all无法在数组中工作

时间:2015-08-11 09:08:21

标签: php

我想在搜索数组后显示结果,但只有标题作为结果显示,但我想显示更多值。

1)如果我使用下面的代码,那么整个数据就会出现在长字符串中,如

Array
(
[] => SONY CDX-G1150U - CD, USB, FM/AM Tuner with MP3/WMA Playback (Single DIN)59902 Pre-Out (Rear + Sub Switchable) Digital Clarity Tuner Subwoofer Easy Connection EQ3 stage 2 / Bollywood EQ mode / Illumination - Red 55W x 4 Output Power (Dynamic Reality Amp 2) Steering Remote Input Ready Mega Bass 2 Year Sony India Warranty http://www.snapdeal.com/product/sony-cdxg1150u-cd-usb-fmam/914627741http://n1.sdlcdn.com/imgs/a/o/m/SONY-CDX-G1150U-CD-USB-SDL133242160-1-51ec4.jpg311Car Audio & Video310Automotive4017in stock
)

    $r = $array3['title'];
    $r .= $array3['mrp'];
    $r .= $array3['description'];
    $r .= $array3['link'];
    $r .= $array3['image_link'];
    $r .= $array3['sub_category_id'];
    $r .= $array3['sub_category_name'];
    $r .= $array3['category_id'];
    $r .= $array3['category_name'];
    $r .= $array3['offer_price'];
    $r .= $array3['availability'];

$matches = array();

$pattern = "/\bSony\b/";
if(preg_match($pattern,$r) || preg_match($pattern,$ss) ){
        $matches[$key]=$r;
    echo "<pre>";print_r($matches); 

2)如果我使用下面的代码(带或条件),那么也会显示相同的结果。

$r = $array3['title'];
$ss = $array3['mrp'];

$matches = array();

$pattern = "/\bSony\b/";
if(preg_match($pattern,$r) || preg_match($pattern,$ss) ){
        $matches[$key]=$r;
    echo "<pre>";print_r($matches); 

1 个答案:

答案 0 :(得分:0)

如果我找对你,请尝试使用

之类的东西
$matches = array();
foreach($array3 as $key => $value) {
    if(preg_match($pattern, $value)) {
        $matches[] = $array3 
    }
}
print_r($matches)

或者在你的问题中更明确