Yii 中的 CMap::mergeArray

发布于 2024-11-19 11:30:46 字数 100 浏览 0 评论 0原文

我知道 Yii 中的 CMap::mergeArray 合并两个数组。但我有三个数组,我想让它们通过 CMap::mergeArray 合并。那么如何在 Yii 框架中合并所有三个数组。

I know CMap::mergeArray in Yii merges two array. But I have three array and I want to make them merge through CMap::mergeArray. So how to merge all the three array in Yii framework.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

揽月 2024-11-26 11:30:46

这取决于你想要什么。需要注意的主要区别是 CMap 会覆盖现有的键,这与内置的 php 函数不同:

如果每个数组都有一个具有相同字符串键值的元素,则
后者将覆盖前者(不同于
数组合并递归)

如果您按照上面的方式执行 CMap::mergeWith,您将得到一个由 3 个数组作为子元素组成的新数组:

例如,给出以下结构:

$foo = array (
    'a' => 1,
    'b' => 2,
    'c' => 3,
    'z' => 'does not exist in other arrays',
    );

$bar = array (
    'a' => 'one',
    'b' => 'two', 
    'c' => 'three',
);

$arr = array (
    'a' => 'uno',
    'b' => 'dos',
    'c' => 'tres',
);

按如下方式使用 CMap::mergeWith:

$map = new CMap();
$map->mergeWith (array($foo, $bar, $arr));
print_r($map);

结果如下:

CMap Object
(
    [_d:CMap:private] => Array
        (
            [0] => Array
                (
                    [a] => 1
                    [b] => 2
                    [c] => 3
                    [z] => does not exist in other arrays
                )

            [1] => Array
                (
                    [a] => one
                    [b] => two
                    [c] => three
                )

            [2] => Array
                (
                    [a] => uno
                    [b] => dos
                    [c] => tres
                )

        )

    [_r:CMap:private] => 
    [_e:CComponent:private] => 
    [_m:CComponent:private] => 
)

如果覆盖元素是首选行为,则以下操作将起作用:

$map = new CMap ($foo);
$map->mergeWith ($bar);
$map->mergeWith ($arr);
print_r($map);

其结果是:

CMap Object
(
    [_d:CMap:private] => Array
        (
            [a] => uno
            [b] => dos
            [c] => tres
            [z] => does not exist in other arrays
        )

    [_r:CMap:private] => 
    [_e:CComponent:private] => 
    [_m:CComponent:private] => 
)

但是,如果您想要附加数据,则:

$map = new CMap (array_merge_recursive ($foo, $bar, $arr));
print_r($map);

将帮助您:

CMap Object
(
    [_d:CMap:private] => Array
        (
            [a] => Array
                (
                    [0] => 1
                    [1] => one
                    [2] => uno
                )

            [b] => Array
                (
                    [0] => 2
                    [1] => two
                    [2] => dos
                )

            [c] => Array
                (
                    [0] => 3
                    [1] => three
                    [2] => tres
                )

            [z] => does not exist in other arrays
        )

    [_r:CMap:private] => 
    [_e:CComponent:private] => 
    [_m:CComponent:private] => 
)

It depends on what you want. The key difference to note is that CMap will overwrite existing keys, which is different from the built in php functions:

If each array has an element with the same string key value, the
latter will overwrite the former (different from
array_merge_recursive)

If you do CMap::mergeWith as above, you'll just be getting a new array consisting of the 3 arrays as subelements:

For example, given the following structure:

$foo = array (
    'a' => 1,
    'b' => 2,
    'c' => 3,
    'z' => 'does not exist in other arrays',
    );

$bar = array (
    'a' => 'one',
    'b' => 'two', 
    'c' => 'three',
);

$arr = array (
    'a' => 'uno',
    'b' => 'dos',
    'c' => 'tres',
);

Using CMap::mergeWith as follows:

$map = new CMap();
$map->mergeWith (array($foo, $bar, $arr));
print_r($map);

Results in the following:

CMap Object
(
    [_d:CMap:private] => Array
        (
            [0] => Array
                (
                    [a] => 1
                    [b] => 2
                    [c] => 3
                    [z] => does not exist in other arrays
                )

            [1] => Array
                (
                    [a] => one
                    [b] => two
                    [c] => three
                )

            [2] => Array
                (
                    [a] => uno
                    [b] => dos
                    [c] => tres
                )

        )

    [_r:CMap:private] => 
    [_e:CComponent:private] => 
    [_m:CComponent:private] => 
)

If overwriting elements is the preferred behavior, then the following will work:

$map = new CMap ($foo);
$map->mergeWith ($bar);
$map->mergeWith ($arr);
print_r($map);

Which results in:

CMap Object
(
    [_d:CMap:private] => Array
        (
            [a] => uno
            [b] => dos
            [c] => tres
            [z] => does not exist in other arrays
        )

    [_r:CMap:private] => 
    [_e:CComponent:private] => 
    [_m:CComponent:private] => 
)

If, however, you want the data appended, then:

$map = new CMap (array_merge_recursive ($foo, $bar, $arr));
print_r($map);

Will get you there:

CMap Object
(
    [_d:CMap:private] => Array
        (
            [a] => Array
                (
                    [0] => 1
                    [1] => one
                    [2] => uno
                )

            [b] => Array
                (
                    [0] => 2
                    [1] => two
                    [2] => dos
                )

            [c] => Array
                (
                    [0] => 3
                    [1] => three
                    [2] => tres
                )

            [z] => does not exist in other arrays
        )

    [_r:CMap:private] => 
    [_e:CComponent:private] => 
    [_m:CComponent:private] => 
)
哆啦不做梦 2024-11-26 11:30:46

使用 array_merge($ar1, $ar2, $ar3)array_merge_recursive($ar1, $ar2, $ar3)

你也可以使用:

CMap::mergeWith(array($ar1, $ar2, $ar3));

但我不确定前者的结果

Use array_merge($ar1, $ar2, $ar3) OR array_merge_recursive($ar1, $ar2, $ar3)

Also you could use:

CMap::mergeWith(array($ar1, $ar2, $ar3));

But I am not sure for the result of the former

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文