【发布时间】:2012-02-12 19:47:02
【问题描述】:
我有一个数组。
在某个阶段,我正在向它添加更多数据。
所以我们有:
$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');
这太白痴了。我将相同的值 twice 粘贴到数组中。我真正想要的是:
$valuesInEditable = Set::combine($editable, 'BodyPartsVideo.{n}.body_part_id',True);
或类似的东西。所以这个问题的重点是:
如何在 cakephp 中使用 Set:combine 设置默认值。如果你有更好的建议,我很乐意听到。
【问题讨论】:
标签: cakephp