說我有標籤如何在PHP中使用array_filter()進行函數式編程?
$all_tags = array('A', 'B', 'C');
一個數組,我想創建一個$ _GET變量URL集。
我想的鏈接是:
'A'
鏈接到"index.php?x[]=B&x[]=C"
'B'
鏈接到"index.php?x[]=A&x[]=C"
等($ _ GET是除了「當前」元素中的所有元素的數組) (我知道有一個更簡單方式來實現這一點:我實際上是簡化更復雜的情況)
我想使用array_filter()
來解決這個問題。
這裏是我的嘗試:
function make_get ($tag) { return 'x[]=' . $tag; }
function tag_to_url ($tag_name) {
global $all_tags;
$filta = create_function('$x', 'global $all_tags; return ($x != $tag_name);');
return 'index.php?' . implode('&', array_map("make_get", array_filter($all_tags, "filta")));
}
print_r(array_map("", $all_tags));
但它不工作。我懷疑它可能與PHP中的映射和過濾器實際上是如何改變數據結構本身有關,並且返回一個布爾值,而不是使用函數式樣,它們不會改變並返回一個新列表。
我也對其他方法使這段代碼更簡潔。
so ...無論鏈接是什麼,從鏈接集合中刪除該特定條目,然後用剩餘的值構建URL? –
也許這樣:http://www.ideone.com/vVnWe? –
@BradChristie:正確 – amindfv