Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CakePHP使用';返回find(';all';);id';as数组索引_Php_Arrays_Cakephp_Data Structures_Find - Fatal编程技术网

CakePHP使用';返回find(';all';);id';as数组索引

CakePHP使用';返回find(';all';);id';as数组索引,php,arrays,cakephp,data-structures,find,Php,Arrays,Cakephp,Data Structures,Find,因此,我尝试返回一个find“all”数组,使用“Product”的id作为每个产品的索引键 通常,它返回: array( (int) 0 => array( 'Product' => array( 'id' => '1232', 'category_id' => '330', 'name' => 'Product #1', ) ), (i

因此,我尝试返回一个find“all”数组,使用“Product”的id作为每个产品的索引键

通常,它返回:

array(
    (int) 0 => array(
        'Product' => array(
            'id' => '1232',
            'category_id' => '330',
            'name' => 'Product #1',
        )
    ),
    (int) 1 => array(
        'Product' => array(
            'id' => '1245',
            'category_id' => '310',
            'name' => 'Product #2',
        )
    ),
    (int) 2 => array(
        'Product' => array(
            'id' => '1248',
            'category_id' => '312',
            'name' => 'Product #3',
        )
    )
)
而理想情况下,我希望它能够返回:

array(
    (int) 1232 => array(
        'Product' => array(
            'id' => '1232',
            'category_id' => '330',
            'name' => 'Product #1',
        )
    ),
    (int) 1245 => array(
        'Product' => array(
            'id' => '1245',
            'category_id' => '310',
            'name' => 'Product #2',
        )
    ),
    (int) 1248 => array(
        'Product' => array(
            'id' => '1248',
            'category_id' => '312',
            'name' => 'Product #3',
        )
    )
)
这可能吗?我该怎么做呢

我有一个需要匹配ID的excel记录电子表格,所以目前我让它迭代每个电子表格行并执行“第一次”查找以获得任何结果,如果有,则对其进行操作

它可以工作,但是记录集已经增长到28000多个,因此为每个记录集执行一次查找具有明显的开销


如果我能在“查找”操作中简单地做到这一点,那就太好了,如果不能,性能的任何显著提高都将受到赞赏。

您可以在“查找所有内容”之后获得此结果:

$result = Hash::combine($data, '{n}.Product.id', '{n}.Product');

其中$data是find all的结果。

您可以在AppModel中像这样扩展find方法

public function find($type = 'first', $query = array()) {
    switch($type) {
        case 'keysid':

            if($results = parent::find('all', $query)){

                if(isset($results[0][$this->alias]['id'])){

                    return Hash::combine($results, '{n}.'.$this->alias.'.id', '{n}');
                }else{
                    return $results;
                }

            } 
        break;
        default:
            return parent::find($type, $query);
            break;
    }
}
之后,您可以按如下方式调用find方法:

$this->Product->find('keysid');
然后您将拥有一个指定的结果数组。但是,如果您需要一行代码,也可以使用一行代码来完成。假设$products是find('all')中的数组


看看这个实用程序

它的性能如何?
if($products = $this->Product->find('all')){
        $alteredProducts = Hash::combine($products, '{n}.Product.id', '{n}');
    }