2012-10-14 85 views
0

我發現堆棧溢出此功能another question,但我想的東西澄清:PHP搜索功能澄清

function sort_comments($ar) 
{ 
    $comments = array(); 
    foreach($ar as $item) 
    { 
     if(is_null($item['parent_id'])) $comments[] = $item; 
     else 
     { 
       $parent_array = array_search_key($item['parent_id'],$comments,'id'); 
       if($parent_array !== false) $comments[$parent_array]['replies'][] = $item; 
     } 
    } 
    return $comments; 
} 

有人能解釋一下傳遞給array_searched_key()的參數?我在php.net中搜索這個函數,但沒有找到它。同樣,我對這些參數有些困惑,特別是爲什麼$ comment數組被傳遞給它。

+0

我會在這裏使用遞歸,而不是 –

+0

這是在WordPress,我認爲這是,但會很好,如果你可以確認。 – Kev

回答

0

首先,這不是PHP的核心功能。這是一個專門爲顯示排序評論而構建的Wordpress函數。

但有一個簡單的解釋我的理解是:

First argument: the ID to search (the query) 
Second argument: array to search in (the datas) 
Third argument: the column to search in (in the array) 

據我瞭解,這是這一點。

0

如果你鏈接了相關的StackOverflow線程,它會把事情放在上下文中。我的猜測是它是this implementation,或類似的。這個函數並不是PHP原生的,不知道它的源代碼是不可能回答的。

0

我不認爲這是在這種情況下使用的WordPress功能 - 我在WordPress codebase中唯一可以找到該功能的地方只接受兩個參數。

我寧願認爲他們指的是我在pastebin上找到的其他功能。遺憾的是,筆者並沒有提供描述參數評論,但我們有:

$needle - a value to match inside the array of arrays being searched 
$haystack - an array of arrays being searched 
$haystackKey - the key within the inner arrays which we want to find in the array of arrays 
$strict - if set to true (default false) then type matching is enforced 

所以,如果密鑰和數據對可以位於內陣列中的至少一個,而如果爲假的函數返回true不。