SMARTY删除具有相同值的数组

时间:2017-08-08 11:48:32

标签: php smarty

我试图找到类似但不成功的东西。如果$ color.code重复,我需要删除一个数组。

代码:

merge_fn <- function(column, data=X, lookup=LU)
{
    value_nm <- paste0(gsub("code", "", column),
                      "value")

    X1 <- merge(data, LU[field == column],
               by.x=c("datayear", column),
               by.y=c("datayear", "code"))

    setnames(X1, "lookupvalue", value_nm)
    X1[, !"field", with=FALSE]
}

M <- merge_fn("subjectcode", data=merge_fn("nationalitycode"))
setkey(M, "id")
M
#     datayear subjectcode nationalitycode id nationalityvalue subjectvalue
#  1:     2007           2               1  1            Irish    Geography
#  2:     2007           5               1  2            Irish         P.E.
#  3:     2007           5               1  3            Irish         P.E.
#  4:     2007           5               2  4         Scottish         P.E.
#  5:     2007           2               3  5           Indian    Geography
#  6:     2008           5               5  6          Italian        Latin
#  7:     2008           4               4  7           French         H.E.
#  8:     2008           2               3  8          Chinese   Beekeeping
#  9:     2008           1               2  9         Scottish       Sewing
# 10:     2008           4               1 10          English         H.E.

当我调试($ color)时,我得到数组:

阵 (     [code] =&gt; 333333 )

阵 (     [code] =&gt; 333333 )

阵 (     [code] =&gt; ABDB25 )

如果代码相同,我只需要获得一次。有人可以帮助我或举例说明我该如何做到这一点?

1 个答案:

答案 0 :(得分:0)

构建数组时,将密钥设为code $modification['colors'][$color['code']] = $color;。它会自动创建唯一键阵列。

另一种选择是使用code pool:

$pool = [];
$newColors = [];

foreach ($modification['colors'] as $color) {
    if (!isset($pool[$color['code']])) {
        $newColors[] = $color;
        $pool[$color['code']] = true;
    }
}

$modification['colors'] = $newColors;