0

I have 2 arrays:

$bigArr = array(
    'simple'=>1
    'advanced'=>array(
        'advanceSimple'=>1,
        'advanceadvance'=>array(
            'simple'=>1
        )  
    )
)

$overide = array(
    'advanced'=>array(
        'advanceSimple'=>2,
        'extra'=>5
    )
)

the merge of those 2 should be:

$bigArr = array(
    'simple'=>1
    'advanced'=>array(
        'advanceSimple'=>2,
        'extra'=>5,
        'advanceadvance'=>array(
            'simple'=>1
        )  
    )
)

you see that the overiden of the small array will overide only where the keys exist, and will add data where it doesnt exist.

I tried many ways of recursive iterator and loops, but still no go.

Do you have any ideas or similar workarounds.

2
  • Have you tried using array_merge? Commented Oct 21, 2013 at 13:17
  • 1
    may be what you need is array_merge_recursive Commented Oct 21, 2013 at 13:23

2 Answers 2

1
$bigArr = array(
    'simple'=>1,
    'advanced'=>array(
        'advanceSimple'=>1,
        'advanceadvance'=>array(
            'simple'=>1
        )  
    )
);
$overide = array(
    'advanced'=>array(
        'advanceSimple'=>2,
        'extra'=>5
    )
);
$res = array_merge_recursive($bigArr,$overide);

is that what you need?

Sign up to request clarification or add additional context in comments.

1 Comment

its not as the required result.. advanceSimple becomes array with entries.. instead of integer
0

Answer for PHP 5.3 +

$bigArr = array(
    'simple'=>1,
    'advanced'=>array(
        'advanceSimple'=>1,
        'advanceadvance'=>array(
            'simple'=>1
        )
    )
);
$overide = array(
    'advanced'=>array(
        'advanceSimple'=>2,
        'extra'=>5
    )
);
$array = array_replace_recursive($bigArr,$overide);
echo "<pre>";
print_R($array);

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.