2015-09-09 46 views
1

蛋糕PHP和MVC的新手,I read this Cake小册子中给出的集合教程。当我打电话从控制器(示例代码),该代码返回像蛋糕PHP:3.0-使用集合

蛋糕\收藏\集合对象()

这实际上应该返回类似这样的[2,3,1]

请纠正我,如果我有错返回代码或任何命名空间已经被冷落

示例代码

<?php 
    namespace App\Controller; 
    use Cake\ORM\TableRegistry; 
    use Cake\Collection\Collection; 
    class AdminController extends AppController 
    { 
    public function collection() 
     { 
     $items = ['a' => 1, 'b' => 2, 'c' => 3]; 
     $collection = new Collection($items); 


     // This could return [2, 3, 1] 
     $collection->shuffle()->toArray(); 
     print_r($collection); exit; 
    } 
    } 
    ?> 

回答

2

您需要检索和输出数组由toArray返回,而不是Collection

$arr = $collection->shuffle()->toArray() ; 
print_r ($arr) ; 

如果你想看到集合本身,使用debug

debug ($collection->shuffle()) ; 
+0

我想你换货打印'$ arr'。并且请注意''shuffle()'也不会修改集合,它会返回一个新的,混合的。 – ndm

+0

@ndm谢谢,我纠正了我的答案。 – Holt

+0

感谢它为我工作.. –