2012-03-30 49 views
3

如何使蛋糕php查找結果數組鍵改變爲使用蛋糕的PHP值?做蛋糕php查找結果被改變數組鍵使用蛋糕php本身

$videos = $this->Video->find('all'); 

print_r($videos); 

陣列將被

Array 
(
    [0] => Array 
     (
      [Video] => Array 
       (
        [id] => 6 
       ) 

     ) 

    [1] => Array 
     (
      [Video] => Array 
       (
        [id] => 8 
       ) 

     ) 
) 

如何更改與值id像下面

Array 
(
    [6] => Array 
     (
      [Video] => Array 
       (
        [id] => 6 
       ) 

     ) 

    [8] => Array 
     (
      [Video] => Array 
       (
        [id] => 8 
       ) 

     ) 
) 

數組鍵是否有可能通過蛋糕PHP本身?

回答

1

不,它不是。你可以改變afterFind()的結果,查看book.cakephp.org,但我會直接使用這些數據。我沒有看到任何理由增加這些額外的開銷和複雜性,因爲ID無論如何可用。

8

如果您確實需要這種格式的數據,您可以使用afterFind來設置ID,也可以使用CakePHP的Hash/Set類。

試試這個(從CakePHP 2.2)

$videos = $this->Video->find('all'); 
$videos = Hash::combine($videos, '{n}.Video.id', '{n}'); 
debug($videos); 

或者這對於CakePHP的年齡大於2.2

$videos = $this->Video->find('all'); 
$videos = Set::combine($videos, '{n}.Video.id', '{n}'); 
debug($videos); 

欲瞭解更多詳情,請參閱http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html

如果你可以告訴我們,爲什麼你需要的數據採用這種格式,我們也可以爲您提供更好的方法,因爲數據不會以這種方式改變。

1

在CakePHP 2.2,你可以使用

$videos = $this->Video->find('list'); 
pr($videos); 

這將打印的每個值與ID爲關鍵。

我不知道你是否可以在1.3中使用它,但你可以嘗試...