2012-11-14 19 views
-1

結合陣列要檢查我多久相等值是在我的數組數他們:用foreach

$count = array_count_values($array_names); 

要獲得每個鍵/值對我用這個foreach循環:

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

可能的輸出:

2 – Harry Peters 
1 – Winston Meyers 
3 – Jason Williams 

現在我已經得到了與URL的第二陣列:

$urls = array(http://harry-peters.com, http://winston-meyers.com, http://jason-williams.com); 

該數組應該圍繞$key變量被裹得像個

echo $value . '- <a href="' . $url . '">' . $key . '</a><br />'; 

所以我會得到這樣的:

2 – <a href="http://harry-peters.com">Harry Peters</a> 
1 – <a href="http://winston-meyers.com">Winston Meyers</a> 
3 – <a href="http://jason-williams.com">Jason Williams</a> 

但我不知道如何實現對另一個foreach循環第一個循環的URL。

+1

第二個數組與第一個數組的關係如何?你能展示一些輸入數組和期望輸出的例子嗎? – raina77ow

+0

是的,我不知道你的意思是第二陣列或網址,樣本輸入和預期輸出會有幫助。 – Landon

+0

我編輯了我的問題... – user1706680

回答

0

的這是對你有用嗎?

<?php 

$names = array 
(
    2 => "Harry Peters", 
    1 => "Winston Meyers", 
    3 => "Jason Williams" 
); 

$urls = array 
(
    "http://harry-peters.com", 
    "http://winston-meyers.com", 
    "http://jason-williams.com" 
); 

$counter = 0; 

foreach ($names as $key => $value) 
{ 
    echo $key . '- <a href="' . $urls[$counter] . '">' . $value . '</a><br />'; 

    $counter++; 
} 

請小心,因爲網址必須與名稱的順序完全一致。

0
$count =array("mark Ortiz", "You", "me", "hello"); 
    $urls = array("http://harry-peters.com", "http://winston-meyers.com", "http://jason-williams.com") 
    foreach ($count as $key => $value) { 
    //echo $key . '-' . $value . '<br />'; 
    $count[$key] = "<a href='$urls[$key]' >".$value."</a>";// this forced value to be formatted 
    } 
    foreach ($count as $key => $value) { 
    echo $key . '-' . $value . '<br />'; 

    } 

,應該是容易的,我希望你要小心,如果以往任何時候都避免數組索引出界外