2012-09-25 120 views
0

如何爆炸數組中的每個元素?php爆炸數組中的元素

Array 
(
    [0] => countryId:2, productId:1, status:1, pId:18, xId:pdgje5566 
) 

請參閱下面的上述陣列:

$searchfor = $xId; 

header('Content-Type: text/plain'); 

$contents = file_get_contents($file); 
$pattern = preg_quote($searchfor, '/'); 
$pattern = "/^.*$pattern.*\$/m"; 

// search, and store all matching occurences in $matches 
if(preg_match_all($pattern, $contents, $matches)){ 

    print_r($matches[0]); 
    //echo implode("\n", $matches[0]); 

} 

請指教,謝謝。

+1

數組中的每個元素都是一個字符串,還是那裏的2D數組的JSON表示?你還想分手什麼? – Sammaye

+0

@Sammaye數據來自文本文件 – conmen

回答

8
foreach ($array as &$value) { 
    $arr = array(); 
    foreach (explode(', ', $value) as $el) { 
    $ret = explode(':', $el); 
    arr[$ret[0]] = $ret[1]; 
    } 
    $value = $arr; 
}