PHP用来自不同数组的值替换数组中的值

时间:2019-01-16 21:30:15

标签: php arrays

我有两个数组,其中408行的array1和256行的array2。

这是array1的saml示例

Array
(
    [0] => Array
        (
            [nodo] => N_127y4Gral
            [int_id] => 34213897
            [dow_id] => Array
                (
                    [0] => 536870914
                    [1] => 536870914
                    [2] => 536870914
                    [3] => 536870914
                    [4] => 536870914
                )
        )
    [1] => Array
        (
            [nodo] => N_121y7
            [int_id] => 34213905
            [dow_id] => Array
                (
                    [0] => 536870915
                    [1] => 536870915
                    [2] => 536870915
                    [3] => 536870915
                    [4] => 536870915
                )
        )
    [2] => Array
        (
            [nodo] => N_144y4Gral
            [int_id] => 34213921
            [dow_id] => Array
                (
                    [0] => 536870918
                    [1] => 536870918
                    [2] => 536870918
                    [3] => 536870918
                    [4] => 536870918
                )
        )
    [3] => Array
        (
            [nodo] => N_132y2
            [int_id] => 34213921
            [dow_id] => Array
                (
                    [0] => 536870914
                    [1] => 536870914
                    [2] => 536870914
                    [3] => 536870914
                    [4] => 536870914
                )
        )
)

这是array2的saml示例

Array
(
    [0] => Array([536870914] => 201920520)
    [1] => Array([536870914] => 201920528)
    [2] => Array([536870914] => 201920536)
    [3] => Array([536870914] => 201920544)
    [4] => Array([536870914] => 201920552)
    [5] => Array([536870915] => 201920560)
    [6] => Array([536870915] => 201920568)
    [7] => Array([536870915] => 201920576)
    [8] => Array([536870915] => 201920584)
    [9] => Array([536870915] => 201920592)
    [10] => Array([536870916] => 201928784)
    [11] => Array([536870916] => 201928792)
    [12] => Array([536870916] => 201928800)
    [13] => Array([536870916] => 201928808)
    [14] => Array([536870916] => 201928816)
    [15] => Array([536870917] => 201928824)
    [16] => Array([536870917] => 201928832)
    [17] => Array([536870917] => 201932808)
    [18] => Array([536870917] => 201932816)
    [19] => Array([536870917] => 201932824)
    [20] => Array([536870918] => 201920600)
    [21] => Array([536870918] => 201920608)
    [22] => Array([536870918] => 201920616)
    [23] => Array([536870918] => 201920624)
    [24] => Array([536870918] => 201920632)
)

我要弄清楚的是如何用array2中的相应值替换array1中的dow_id值。所有的dow_id值都以id的形式存在于array2中。

这是我想要实现的输出

Array
(
    [0] => Array
        (
            [nodo] => N_127y4Gral
            [int_id] => 34213897
            [dow_id] => Array
                (
                    [0] => 201920520
                    [1] => 201920528
                    [2] => 201920536
                    [3] => 201920544
                    [4] => 201920552
                )
        )
    [1] => Array
        (
            [nodo] => N_121y7
            [int_id] => 34213905
            [dow_id] => Array
                (
                    [0] => 201920560
                    [1] => 201920568
                    [2] => 201920576
                    [3] => 201920584
                    [4] => 201920592
                )
        )
    [2] => Array
        (
            [nodo] => N_144y4Gral
            [int_id] => 34213921
            [dow_id] => Array
                (
                    [0] => 201920600
                    [1] => 201920608
                    [2] => 201920616
                    [3] => 201920624
                    [4] => 201920632
                )
        )
    [3] => Array
        (
            [nodo] => N_132y2
            [int_id] => 34213854
            [dow_id] => Array
                (
                    [0] => 201920520
                    [1] => 201920528
                    [2] => 201920536
                    [3] => 201920544
                    [4] => 201920552
                )
        )            
)

任何帮助或建议都将不胜感激

4 个答案:

答案 0 :(得分:1)

如果您将$array2更改为此结构:

$array2 = Array(536870914 => Array(201920520, 201920528, 201920536, 201920544, 201920552));

然后循环并用当前值索引的数组替换:

foreach($array1 as &$values) {
    $values['dow_id'] = $array2[reset($values['dow_id')];   
}

如果将$array2更改为此结构:

$array2 = Array(201920520 => 536870914, 
                201920528 => 536870914, 
                201920536 => 536870914, 
                201920544 => 536870914, 
                536870914 => 536870914);

然后循环并提取值等于当前值的索引:

foreach($array1 as &$values) {
    $values['dow_id'] = array_keys($values['dow_id'], reset($values['dow_id']));
}

答案 1 :(得分:1)

使用array_column确实很容易做到。无需更改数组格式:

foreach ($a1 as &$value) {
    $value['dow_id'] = array_column($a2, $value['dow_id'][0]);
}

Demo on 3v4l.org

答案 2 :(得分:0)

类似这样的东西:

$a1 = array (
    0 => 
    array (
        'nodo' => 'N_127y4Gral',
        'int_id' => 34213897,
        'dow_id' => 
        array (
            0 => 536870914,
            1 => 536870914,
            2 => 536870914,
            3 => 536870914,
            4 => 536870914
        ),

    ),
    1 => 
    array (
        'nodo' => 'N_121y7',
        'int_id' => 34213905,
        'dow_id' => 
        array (
            0 => 536870915,
            1 => 536870915,
            2 => 536870915,
            3 => 536870915,
            4 => 536870915
        ),

    ),
    2 => 
    array (
        'nodo' => 'N_144y4Gral',
        'int_id' => 34213921,
        'dow_id' => 
        array (
            0 => 536870918,
            1 => 536870918,
            2 => 536870918,
            3 => 536870918,
            4 => 536870918
        ),

    ),
    3 => 
    array (
        'nodo' => 'N_132y2',
        'int_id' => 34213921,
        'dow_id' => 
        array (
            0 => 536870914,
            1 => 536870914,
            2 => 536870914,
            3 => 536870914,
            4 => 536870914
        ),

    )
);

$a2 = array (
    0 => 
    array (
        536870914 => 201920520
    ),
    1 => 
    array (
        536870914 => 201920528
    ),
    2 => 
    array (
        536870914 => 201920536
    ),
    3 => 
    array (
        536870914 => 201920544
    ),
    4 => 
    array (
        536870914 => 201920552
    ),
    5 => 
    array (
        536870915 => 201920560
    ),
    6 => 
    array (
        536870915 => 201920568
    ),
    7 => 
    array (
        536870915 => 201920576
    ),
    8 => 
    array (
        536870915 => 201920584
    ),
    9 => 
    array (
        536870915 => 201920592
    ),
    10 => 
    array (
        536870916 => 201928784
    ),
    11 => 
    array (
        536870916 => 201928792
    ),
    12 => 
    array (
        536870916 => 201928800
    ),
    13 => 
    array (
        536870916 => 201928808
    ),
    14 => 
    array (
        536870916 => 201928816
    ),
    15 => 
    array (
        536870917 => 201928824
    ),
    16 => 
    array (
        536870917 => 201928832
    ),
    17 => 
    array (
        536870917 => 201932808
    ),
    18 => 
    array (
        536870917 => 201932816
    ),
    19 => 
    array (
        536870917 => 201932824
    ),
    20 => 
    array (
        536870918 => 201920600
    ),
    21 => 
    array (
        536870918 => 201920608
    ),
    22 => 
    array (
        536870918 => 201920616
    ),
    23 => 
    array (
        536870918 => 201920624
    ),
    24 => 
    array (
        536870918 => 201920632
    )
);

和代码(为便于阅读而分开)

$o = [];

#convert the structure of the second array
foreach($a2 as $k=>$v){
    #eg. $v = [536870914 => 201920520]
    $key = key($v); #get the first key 
    $o[$key][] = reset($v); #get the first value 
}

#code to replace (simple right), I am assuming that dow_id contains only 1 id (in duplicate)
foreach( $a1 as $k=>&$v){
    if(isset($o[$v['dow_id'][0]]))  $v['dow_id'] = $o[$v['dow_id'][0]];
}

print_r($a1);

输出

Array
(
    [0] => Array
        (
            [nodo] => N_127y4Gral
            [int_id] => 34213897
            [dow_id] => Array
                (
                    [0] => 201920520
                    [1] => 201920528
                    [2] => 201920536
                    [3] => 201920544
                    [4] => 201920552
                )

        )

    [1] => Array
        (
            [nodo] => N_121y7
            [int_id] => 34213905
            [dow_id] => Array
                (
                    [0] => 201920560
                    [1] => 201920568
                    [2] => 201920576
                    [3] => 201920584
                    [4] => 201920592
                )

        )

    [2] => Array
        (
            [nodo] => N_144y4Gral
            [int_id] => 34213921
            [dow_id] => Array
                (
                    [0] => 201920600
                    [1] => 201920608
                    [2] => 201920616
                    [3] => 201920624
                    [4] => 201920632
                )

        )

    [3] => Array
        (
            [nodo] => N_132y2
            [int_id] => 34213921
            [dow_id] => Array
                (
                    [0] => 201920520
                    [1] => 201920528
                    [2] => 201920536
                    [3] => 201920544
                    [4] => 201920552
                )

        )

)

Sandbox

PS。我将您的print_r输出转换为var_export,var export是有效的PHP语法。打印R不是。您可以在我的website上找到转换器。我构建了它,只要不修改输出,它就可以很好地工作。抱歉,如果我的网站有点“简陋”,我永远都没有动力去工作...大声笑。

答案 3 :(得分:0)

$arr = [
    [

        'nodo'   => 'N_127y4Gral',
        'int_id' => 34213897,
        'dow_id' => [
            0 => 536870914,
            1 => 536870914,
            2 => 536870914,
            3 => 536870914,
            4 => 536870914,
        ]
    ],
    [

        'nodo'   => 'N_121y7',
        'int_id' => 34213905,
        'dow_id' => [
            0 => 536870915,
            1 => 536870915,
            2 => 536870915,
            3 => 536870915,
            4 => 536870915,
        ]
    ],
    [

        'nodo'   => 'N_144y4Gral',
        'int_id' => 34213921,
        'dow_id' => [
            0 => 536870918,
            1 => 536870918,
            2 => 536870918,
            3 => 536870918,
            4 => 536870918,
        ]
    ],
];

$childArray = [
    0  => [536870914 => 201920520],
    1  => [536870914 => 201920528],
    2  => [536870914 => 201920536],
    3  => [536870914 => 201920544],
    4  => [536870914 => 201920552],
    5  => [536870915 => 201920560],
    6  => [536870915 => 201920568],
    7  => [536870915 => 201920576],
    8  => [536870915 => 201920584],
    9  => [536870915 => 201920592],
    20 => [536870918 => 201920600],
    21 => [536870918 => 201920608],
    22 => [536870918 => 201920616],
    23 => [536870918 => 201920624],
    24 => [536870918 => 201920632],
];

/**
 * grouping
 * @param array $last
 * @return array
 */
function groupArray(array $last)
{
    $result = [];
    foreach ($last as $value) {
        $result[key($value)][] = current($value);
    }
    return $result;
}

/**
 * @param array $first
 * @param array $last
 * @return array
 */
function linkArray(array $first, array $last)
{
    $result = [];
    foreach ($first as $id => $fValue) {
        if (empty($fValue['dow_id'])) {
            continue;
        }
        $dow = [];
        foreach ($fValue['dow_id'] as $dowId => $dowValue) {
            $dow[$dowId] = array_shift($last[$dowValue]);
        }
        $result[$id] = [
            'nodo'   => $fValue['nodo'] ?? '',
            'int_id' => $fValue['int_id'] ?? 0,
            'dow_id' => $dow
        ];
    }
    return $result;
}

$result = linkArray($arr, groupArray($childArray));
print_r($result);

所以我们有:

Array
(
    [0] => Array
        (
            [nodo] => N_127y4Gral
            [int_id] => 34213897
            [dow_id] => Array
                (
                    [0] => 201920520
                    [1] => 201920528
                    [2] => 201920536
                    [3] => 201920544
                    [4] => 201920552
                )

        )

    [1] => Array
        (
            [nodo] => N_121y7
            [int_id] => 34213905
            [dow_id] => Array
                (
                    [0] => 201920560
                    [1] => 201920568
                    [2] => 201920576
                    [3] => 201920584
                    [4] => 201920592
                )

        )

    [2] => Array
        (
            [nodo] => N_144y4Gral
            [int_id] => 34213921
            [dow_id] => Array
                (
                    [0] => 201920600
                    [1] => 201920608
                    [2] => 201920616
                    [3] => 201920624
                    [4] => 201920632
                )

        )

)

因此,即使您错过了元素,您也将在结果数组中收到空元素。 在两个阵列中,您都有重复项。您应该优化它们的结构。