2014-02-26 44 views
0

Set::extract是一個非常強大的函數,用於過濾從find方法檢索到的數據。 但是,據我所知,結果不是嵌套的。我的意思是,如果我有一個像Cakephp使用Set來過濾來自查詢的結果

data = 
Array (
0 => array(
    'User' => Array (...), 
    'Profile => Array (...) 
    ) 
1 => array(
    'User' => Array (...), 
    'Profile => Array (...) 
    ) 
) 

一個數組,我會提取所有的用戶,比方說,User.type =1,我會做:

$filtered Set::extract('/User[type=1',$data). 

$filtered = Array(
0 => Array (
    'User' => Array() 
) 
) 

和其他陣列,配置文件,不顯示。使用提取進行過濾時可以獲取所有數據嗎?

回答

1

您可以在第一個參數中使用/../遍歷數組路徑。

嘗試

$filtered = Set::extract('/User[type=1]/..',$data); 

來源:http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::extract

它說,這是基本的XPath 2.0語法和其中一個例子使用它。

+0

太棒了!你能指出我在文件解釋'..'的位置嗎? – giuseppe

+0

哇真的這是最好的答案。 – Andolasoft

+0

另一個問題是如何強制分頁程序使用過濾結果.... – giuseppe