arrays
  • placeholder
  • 2012-11-14 35 views 0 likes 
    0

    我有一個數組,其中佔位符是變量$值,我得到的代碼後的佔位符:陣列佔位符

    $names = array(
        "<a href='http://skyler.com' title='PLACEHOLDER'>Skyler</a>", 
        "<a href='http://jesse.com' title='PLACEHOLDER'>Jesse</a>", 
        "<a href='http://walter.com' title='PLACEHOLDER'>Walter</a>", 
        "<a href='http://skyler.com' title='PLACEHOLDER'>Skyler</a>", 
        "<a href='http://hank.com' title='PLACEHOLDER'>Hank</a>", 
        "<a href='http://marie.com' title='PLACEHOLDER'>Marie</a>", 
        "<a href='http://walter.com' title='PLACEHOLDER'>Walter</a>", 
        "<a href='http://walter.com' title='PLACEHOLDER'>Walter</a>", 
        "<a href='http://jesse.com' title='PLACEHOLDER'>Jesse</a>", 
        ); 
    

    要檢查同等價值如何往往是在我的陣列, 我用array_count_values來計數。

    $count = array_count_values($names); 
    
    foreach ($count as $key => $value) { 
        echo $value . ' – ' . $key . '<br />'; 
    } 
    

    所以我得到的是這樣的:

    3 – <a href='http:/walter.com' title='PLACEHOLDER'>Walter</a> 
    2 – <a href='http://jesse.com' title='PLACEHOLDER'>Jesse</a> 
    2 – <a href='http://skyler.com' title='PLACEHOLDER'>Skyler</a> 
    1 – <a href='http://hank.com' title='PLACEHOLDER'>Hank</a> 
    1 – <a href='http://marie.com' title='PLACEHOLDER'>Marie</a> 
    

    現在我已經預留位置被$值替換,所以我得到的鏈接的標題標籤的數量。

    回答

    1
    foreach ($count as $key => $value) { 
        echo $value . ' – ' . str_replace('PLACEHOLDER', $value, $key) . '<br />'; 
    } 
    
    1

    你在問,我不明白,你要替換$值PLACEHOLD然後做這個

    $names = array(
    "<a href='http://skyler.com' title='".$PLACEHOLDER."'>Skyler</a>", 
    "<a href='http://jesse.com' title='".$PLACEHOLDER."'>Jesse</a>" 
    ); 
    
    +0

    '$ value'是我後來在代碼中獲得的一個數組,所以如果我寫'$ value'而不是PLACEHOLDER,那麼我只在所有標題標籤中得到一個'2'。但'$ value'實際上是一個包含以下內容的數組:'$ value = array(3,2,2,1,1)' – user1706680

    1

    使用str_replace$value

    ​​
    1

    佔位符代替正確的代碼是:

    foreach ($count as $key => $value) { 
        echo str_replace('PLACEHOLDER', $value, $key); //this will replace placeholder with number of tags 
        echo '<br />'; 
    } 
    
    相關問題