1
[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
            )

    )

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

3
  • What if there are multiple elements in lang_2 where status_answer = 2? Commented Apr 3, 2015 at 3:18
  • That will not happen.. But if happen. Just take the value in lang_2. Just want to replace from lang_2 to lang_1 Commented Apr 3, 2015 at 3:43
  • So write a loop that finds the element in lang_2 that has the value you want. Then write a loop that finds the element in lang_1 that you want to replace, and replace it. Commented Apr 3, 2015 at 3:44

1 Answer 1

0

First search lang_2 for the element you want:

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

Then search lang_1 for the element you want to replace:

foreach ($array['lang_1'] as &$el) {
    if ($el['status_answer'] == 2) {
        $el = $replacement;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.