3

I am newbie and have problem to combining array to multidimensional array. I have tested with array_merge but not working fine.

I have array like this:

Array (
    [0] => value 1
    [1] => value 2
    [2] => value 3
    [3] => value 4
)

And multidimensional array like this:

Array
(
    [0] => Array
        (
            [name] => simple
            [label] => Simple
            [filter] => 1
            [filterexcerpt] => 1
        )

    [1] => Array
        (
            [name] => compleks
            [label] => Compleks
            [filter] => 1
            [filterexcerpt] => 1
        )

    [2] => Array
        (
            [name] => standard
            [label] => Standard
            [filter] => 
            [filterexcerpt] => 
        )

    [3] => Array
        (
            [name] => abstract
            [label] => Abstract
            [filter] => 1
            [filterexcerpt] => 1
        )
)

How merge array to multidimensional array, and the result i want like this:

Array
(
    [0] => Array
        (
            [name] => simple
            [label] => Simple
            [filter] => 1
            [filterexcerpt] => 1
            [value] => value 1
        )

    [1] => Array
        (
            [name] => compleks
            [label] => Compleks
            [filter] => 1
            [filterexcerpt] => 1
            [value] => value 2
        )

    [2] => Array
        (
            [name] => standard
            [label] => Standard
            [filter] => 
            [filterexcerpt] => 
            [value] => value 3
        )

    [3] => Array
        (
            [name] => abstract
            [label] => Abstract
            [filter] => 1
            [filterexcerpt] => 1
            [value] => value 4
        )
)

Please help.

1 Answer 1

3

Loop the little array, and put the value of that into the bigger array, where the indexes of both arrays match.

foreach ($small_array as $key=>$value) {
    $multidimensional_array[$key]['value'] = $value);
}

Change $small_array and $multidimensional_array as they appear in your code.

That snippet above doesn't check that the multidimensional array actually have such an index in it, matching the index from the little array. You can add a check with array_key_exist() or isset() before adding it, to make the code more fool-proof.

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.