php - array_combine或array_merge?

I'm sure this is easy for someone well-versed in php, but I've made the mistake of overloading my brain, so now I'm really confused as to whether I should use array_combine, array_merge, or something else... I've been googling and reading php.net for 4 hours and I think I'm just confusing myself even more...

Essentially, I just want to combine an array while keeping the keys?

//Here are the original arrays
[field_sreference] => Array
    (
        [0] => Array
            (
                [nid] => 28
            )

        [1] => Array
            (
                [nid] => 28
            )

        [2] => Array
            (
                [nid] => 29
            )
    )

[field_idelta] => Array
    (
        [0] => Array
            (
                [value] => 0
            )

        [1] => Array
            (
                [value] => 1
            )

        [2] => Array
            (
                [value] => 0
            )
    )

[field_iswitch] => Array
    (
        [0] => Array
            (
                [value] => 0
            )

        [1] => Array
            (
                [value] => 0
            )

        [2] => Array
            (
                [value] => 0
            )
    )

//Here is what I'm trying to achieve:

[combinedarray] => Array
        (
            [0] => Array
                (
                    [nid] => 28
                    [idelta] => 0
                    [iswitch] => 0

                )

            [1] => Array
                (
                    [nid] => 28
                    [idelta] => 1
                    [iswitch] => 0
                )

            [2] => Array
                (
                    [nid] => 29
                    [idelta] => 0
                    [iswitch] => 0
                )
        )
php
doutang1884
doutang1884 array_combine使用一个数组作为键,另一个作为值使用,这对于这里没有帮助。
8 年多之前 回复
douji1853
douji1853 你试过吗?结果是什么?
8 年多之前 回复
doushu8260
doushu8260 发布数组结构($arr=array('field_s...)将允许人们更轻松地帮助您。
8 年多之前 回复

2个回答

You can solve this is O(n) by simply iterating the arrays...

$combinedarray = array();
$len = count($field_sreference);
for ($i = 0; $i < $len; $i++) {
    $combinedarray[] = array("nid" => $field_sreference[$i]['nid'], 
                             "idelta" => $filed_idelta[$i]['value'], 
                             "iswitch" => $field_iswitch[$i]['value']);
}

This assumes, the 3 arrays are all of equal length.

dsds33222
dsds33222 omg ...我应该去睡觉。 谢谢
8 年多之前 回复

A bit quickly typed, but this should work:

$result = array();
foreach ($arrays as $array)
{
    foreach ($array as $index => $data)
    {
        $result[$index] += $data;        
    }
}

As you have not provided some input array in some easy form, you need to test it on your own. Let's say it's pseudo-code and I leave it here as an exercise. The + operator is the array union operator.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐