Jus*_*ohn 3 cakephp cakephp-1.3
如何使用cake php将蛋糕php查找结果数组键更改为id值?
$videos = $this->Video->find('all');
print_r($videos);
Run Code Online (Sandbox Code Playgroud)
阵列将是
Array
(
[0] => Array
(
[Video] => Array
(
[id] => 6
)
)
[1] => Array
(
[Video] => Array
(
[id] => 8
)
)
)
Run Code Online (Sandbox Code Playgroud)
如何使用id值更改数组键,如下所示
Array
(
[6] => Array
(
[Video] => Array
(
[id] => 6
)
)
[8] => Array
(
[Video] => Array
(
[id] => 8
)
)
)
Run Code Online (Sandbox Code Playgroud)
蛋糕php本身有可能吗?
如果您确实需要数据采用此格式,则可以使用afterFind设置ID,也可以使用CakePHP的Hash/ Setclass.
试试这个(因为CakePHP 2.2)
$videos = $this->Video->find('all');
$videos = Hash::combine($videos, '{n}.Video.id', '{n}');
debug($videos);
Run Code Online (Sandbox Code Playgroud)
或者对于早于2.2的CakePHP
$videos = $this->Video->find('all');
$videos = Set::combine($videos, '{n}.Video.id', '{n}');
debug($videos);
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息,请参阅http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html
如果您可能告诉我们为什么您需要采用这种格式的数据,我们也可以建议更好的方法,因为数据不能以这种方式更改.