本文介绍了更换阵列PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[lang_1] => Array
    (
        [0] => Array
            (
                [answer] => satu
                [status_answer] => 2
                [key_array] => 0
                [language] => lang_1
            )

        [1] => Array
            (
                [answer] => dua_en
                [status_answer] => 3
                [key_array] =>
                [language] => lang_1
            )

        [2] => Array
            (
                [answer] => lima
                [status_answer] => 3
                [key_array] =>
                [language] => lang_1
            )

        [3] => Array
            (
                [answer] => enam
                [status_answer] => 3
                [key_array] =>
                [language] => lang_1
            )

    )

[lang_2] => Array
    (
        [0] => Array
            (
                [answer] => satu
                [status_answer] => 3
                [key_array] =>
                [language] => lang_2
            )

        [1] => Array
            (
                [answer] => dua_en
                [status_answer] => 2
                [key_array] => 1
                [language] => lang_2
            )

        [2] => Array
            (
                [answer] => lima
                [status_answer] => 3
                [key_array] =>
                [language] => lang_2
            )

        [3] => Array
            (
                [answer] => enam
                [status_answer] => 3
                [key_array] =>
                [language] => lang_2
            )

    )

我有这个两阵。我的问题是,我怎么能在 LANG_2 ,其中 status_answer =替换 lang_1 与数组的数组2

I have this two array. My problem is, how i can replace array in lang_1 with array in lang_2 where status_answer = 2 only

推荐答案

首先搜索 LANG_2 您需要的元素:

First search lang_2 for the element you want:

foreach ($array['lang_2'] as $el) {
    if ($el['status_answer'] == 2) {
        $replacement = $el;
        break;
    }
}

然后搜索 lang_1 你要替换的元素:

foreach ($array['lang_1'] as &$el) {
    if ($el['status_answer'] == 2) {
        $el = $replacement;
    }
}

这篇关于更换阵列PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 15:46