CakePHP 并设置:与默认值组合

问题描述 投票:0回答:1

我有一个数组。

在某个阶段,我会向其中添加更多数据。

所以我们有:

$editable = someArrayGeneratingFunctionHere();
$points = preg_split('/,/',$this->data['Video']['points']);

可爱。现在,“points”数组有一堆数据,这些数据可能已存在于可编辑数组中,也可能尚未存在。 我想要的是检查数据是否可编辑,如果不可编辑则添加。 我也想高效地做到这一点。

所以,我有这个方法:

private function associateWithRelatedBodyParts($editable, $keysAlreadyPresent, ...){
    $point = getOtherPointsThatAreRelatedToThisPoint();
    if (!isset($keysAlreadyPresent[$point])){
        insertDataIntoEditable();           
    } //else the value is already here. Do not add it again!
    return $editable;
}

所以整个事情看起来像这样:

$editable = someArrayGeneratingFunctionHere();
$points = preg_split('/,/',$this->data['Video']['points']);
$valuesInEditable = ...
foreach ($points as $point){ 
    $editable = $this->associateWithRelatedBodyParts($editable, $valuesInEditable,...);
}   

设置太多了!这一切的整个要点是:

我想展平原始的 $editable 数组,因为这样,我可以快速测试一个点是否在可编辑中。如果没有的话我会补充的。

我当前检索valuesInEditable数组的方法是

$valuesInEditable = Set::combine($editable, 'BodyPartsVideo.{n}.body_part_id','BodyPartsVideo.{n}.body_part_id');

这太白痴了。我将相同的值两次插入数组中。我真正想要的是:

$valuesInEditable = Set::combine($editable, 'BodyPartsVideo.{n}.body_part_id',True);

或者类似的东西。所以这个问题的重点是:

如何在 cakephp 中使用 Set:combine 设置默认值。如果您有更好的建议,我很乐意听到。

php cakephp
1个回答
0
投票

在没有看到数组的结构的情况下,似乎您正在经历大量工作来组合两个数组。有什么原因不能使用吗

$final_array = array_merge($points, $editable);

在运行组合联合收割机之前?

© www.soinside.com 2019 - 2024. All rights reserved.