我有兩個preg_match()調用,我想合併數組而不是替換第一個數組。到目前爲止我的代碼:如何將preg_match的輸出附加到現有數組?
$arr = Array();
$string1 = "Article: graphics card";
$string2 = "Price: 300 Euro";
$regex1 = "/Article[\:] (?P<article>.*)/";
$regex2 = "/Price[\:] (?P<price>[0-9]+) Euro/";
preg_match($regex1, $string1, $arr);
//output here:
$arr['article'] = "graphics card"
$arr['price'] = null
preg_match($regex2, $string2, $arr);
//output here:
$arr['article'] = null
$arr['price'] = "300"
我怎麼可能匹配,所以我的結果應該是:
$arr['article'] = "graphics card"
$arr['price'] = "300"
?
您可以合併兩個正則表達式的匹配邏輯,然後preg_match串聯版本的字符串。 – JamesSwift
介意使用'$ cache = array();'? – Passerby
我不想要一個緩存 – Paedow