2013-08-28 90 views
2

我有數組:的preg_replace PHP KEY <-> VALUE

$array = array(
    '/news/show/([0-9])/([0-9])'=>'/news/show/id/$1/id2/$2', 
    '/home/ayz/([0-9])'=>'/home/xyz/sid/$1' 
); 

我想結果是:

$array = array(
     '/news/show/$1/$2'=>'/news/show/id/([0-9])/id2/([0-9])', 
     '/home/ayz/$1'=>'/home/xyz/sid/([0-9])' 
    ); 

怎麼辦呢?

回答

1
$aReplace = array('$1'=>'([0-9])','$2'=>'([0-9])'); 
$aNewArray = array(); 
foreach ($array as $key=>$value){ 
    $nKey = str_replace(array_values($aReplace), array_keys($aReplace),$key); 
    $nValue = str_replace(array_keys($aReplace), array_values($aReplace),$value); 
    $aNewArray[$nKey]=$nValue; 
} 
相關問題