2012-02-14 14 views
0

Hmmm説明する方法? ;)私はちょうど使いやすさのために切り捨てURL「テンプレートの配列(考える話:)PHP - 別の配列の値から配列内の変数を設定する

$default = array(
    "/$market", 
    "/$market/catalog/productsaz/", 
    "/$market/catalog/products/$artid", 
); 

や市場の多次元配列に安っぽいコーディングをしましょうと思います(簡単にするために切り捨て

市場ごとに解決されたURLの配列を取得するにはどうすればよいですか?もちろんarray_mergeの

すなわち

/gb/en 
/gb/en/catalog/productsaz/ 
/gb/en/catalog/products/4525412 
/at/de 
/at/de/catalog/productsaz/ 
/at/de/catalog/products/8456125 
/be/nl 
/be/nl/catalog/productsaz/ 
/be/nl/catalog/products/4578542 

当然の選択だが、私はそれがこのケースでどのように動作するか動作するように見えることはできません。だから私は効率的ではないが、それは私にはうまくいくはずだが、市場の配列からの変数は解決していないと思われる、より長い手の道を試した。

私はこれを持っているこれまで:

function builder($clusterArray,$marketArray,$urlArray) { 
if(isset($_POST['server'])) { 
    $server = $_POST['server']; 
    // DETERMINE THE CLUSTER 
    $cluster=false; 
    foreach($clusterArray as $n=>$c) 
     if(in_array($server,$c)) { 
      $cluster=$n; 
      break; 
     } 
    print("Server=" . $server . "and cluster=" . $cluster); //DEBUG 
    // DRAW THE TABLE 
    //print("<table><tr><th>MARKET</th>"); 

    // LOOP THROUGH MARKETS AND IDS TO CREATE AN ARRAY OF URL'S 
    $urlsTohit = array(); 
    $baseurl = "http://$server"; 
    foreach($marketArray as $mkt) { 
     $market = $mkt['market']; 
     $storeid = $mkt['storeid']; 
     $langid = $mkt['langid']; 
     $storenumber = $mkt['storenumber']; 
     $prodid = $mkt['prodid']; 
     $artid = $mkt['artid']; 
     foreach($urlArray as $template){ 
      array_push($urlsTohit,$baseurl . $template);   
     } 
    } 
    print_r($urlsTohit); //DEBUG 
} else { 
    print("Oops! Seems like you forgot to select a server. Please go back and choose a server before clicking on Go!"); 
} 
} 

はEDIT:@Eugen Rieckによると、私はあることを私の機能を更新しました:

function preSurfBuilder($clusterArray,$marketArray,$urlArray) { 
if(isset($_POST['server'])) { 
    $server = $_POST['server']; 
    // DETERMINE THE CLUSTER 
    $cluster=false; 
    foreach($clusterArray as $n=>$c) 
     if(in_array($server,$c)) { 
      $cluster=$n; 
      break; 
     } 
    print("Server=" . $server . "and cluster=" . $cluster); //DEBUG 
    // DRAW THE TABLE 
    //print("<table><tr><th>MARKET</th>"); 

    // LOOP THROUGH MARKETS AND IDS TO CREATE AN ARRAY OF URL'S 
    $baseurl = "http://$server"; 
    $result=array(); 
    foreach ($urlArray as $u) { 
     foreach ($marketArray as $m) { 
      $uu=$u; 
      foreach ($m as $k=>$v) 
       $uu=str_replace('$'.$k, $v, $uu); 
      $result[]=$uu; 
      } 
     } 
    print_r($result); 
} else { 
    print("Oops! Seems like you forgot to select a server. Please go back and choose a server before clicking on Go!"); 
} 
} 

と出力が(切り捨て)のようなものです:

Array ([0] =>/[1] =>/[2] =>/[3] =>/[4] =>/[5] =>/[6] =>/[7] =>/[8] =>/[9] =>/[10] =>/[11] =>/[12] =>/[13] =>/[14] =>/[15] =>/[16] =>/[17] =>/[18] =>/[19] =>/[20] =>/[21] =>/[22] =>/[23] =>/[24] =>/[25] =>/[26] =>/[27] =>/[28] =>/[29] =>/[30] =>/[31] =>/[32] =>/[33] =>/[34] =>/[35] =>/[36] =>/[37] => //catalog/productsaz/ [38] => //catalog/productsaz/ [39] => //catalog/productsaz/ [40] => //catalog/productsaz/ [41] => //catalog/productsaz/ [42] => //catalog/productsaz/ [43] => //catalog/productsaz/ [44] => //catalog/productsaz/ [45] => //catalog/productsaz/ [46] => //catalog/productsaz/ [47] => //catalog/productsaz/ [48] => //catalog/productsaz/ [49] => //catalog/productsaz/ [50] => //catalog/productsaz/ [51] => //catalog/productsaz/ [52] => //catalog/productsaz/ [53] => //catalog/productsaz/ [54] => //catalog/productsaz/ [55] => //catalog/productsaz/ [56] => //catalog/productsaz/ [57] => //catalog/productsaz/ [58] => //catalog/productsaz/ [59] => //catalog/productsaz/ [60] => //catalog/productsaz/ [61] => //catalog/productsaz/ [62] => //catalog/productsaz/ [63] => //catalog/productsaz/ [64] => //catalog/productsaz/ [65] => //catalog/productsaz/ [66] => //catalog/productsaz/ [67] => //catalog/productsaz/ [68] => //catalog/productsaz/ [69] => //catalog/productsaz/ [70] => //catalog/productsaz/ [71] => //catalog/productsaz/ [72] => //catalog/productsaz/ [73] => //catalog/p 

答えて

3
$result=array(); 
foreach ($default as $d) { 
    foreach ($markets as $m) { 
    $dd=$d; 
    foreach ($m as $k=>$v) 
     $dd=str_replace('$'.$k, $v, $dd); 
    $result[]=$dd; 
    } 
} 

print_r($result); 
+0

編集:thinko($ dd対$ d) –

+0

aha!私はちょうど返信しようとしていた...私は新しいものを試してみよう... – Seer

+0

結果は '// gb/en'で、友人(最初は二重スラッシュ):あなたのデフォルトは '/ $ market'( 1つのスラッシュ)、$ marketには "/ gb/en"(2番目のスラッシュ)があります。データを削除する必要があります。 –

関連する問題