0

I am wondering is it possible to merge 2 arrays that look this:

$array1 = array("a","b","c");
$array2 = array('c'=>array("blah"=>"5", "moreblah"=>"5"));
$merge = array_merge($array1,$array2);
print_r($merge);

Running this will give me this output:

Array ( [0] => a [1] => b [2] => c [c] => Array ( [blah] => 5 [moreblah] => 5 ) )

But the output I want is something similar to this:

Array( [a]=>Array([blah]=>0, [moreblah]=>0), [b]=>Array([blah]=>0, [moreblah]=>0), [c]=>Array([blah]=>5, [moreblah]=>5))

So for the first array I want the values to become the keys and then blah and moreblah to be added and set as 0 if they are not present in array 2. Also for array 2 if there is a repeat such as c in the example array 2 would just overwrite the c index and create the output I wrote above.

Is this possible? If so can I do it with a built in method or would I have to use a for loop to try get it working?

Edit:As has been pointed out to be it is not possible. Can someone explain what type of function I would need to make to be able to get the output I would want?

3
  • That's not a merge. You're trying to impose the structure of one array onto another using the other's values to put into the "template". That's something you should do with a loop. PHP has a decent selection of array functions, but expecting php to have a dedicated function for EVERY possible array operation, let along every possible array structure is just expecting too much. Commented Feb 28, 2014 at 14:46
  • Just write your own function to recreate an array that looks like the one you want..? Commented Feb 28, 2014 at 14:48
  • @Dieter I am not sure how? Can you give me an idea of how please? Commented Feb 28, 2014 at 14:51

2 Answers 2

1

Something like:

EDIT: This only works when you know the keys in the second array, is this known or not? Else i change the code..

$array1 = array("a", "b", "c");
$array2 = array('c' => array("blah" => "5", "moreblah" => "5"));

$newArray = "";

foreach ($array1 as $a1) {
    if (key_exists($a1, $array2)) {
        //check if blah and / or moreblah is set else set the value to 0 or something else
        if (key_exists("blah", $array2[$a1])) {
            $blah = $array2[$a1]["blah"];
        } else {
            $blah = 0;
        }

        if (key_exists("moreblah", $array2[$a1])) {
            $moreblah = $array2[$a1]["moreblah"];
        } else {
            $moreblah = 0;
        }


        $newArray[$a1] = array("blah" => $blah, "moreblah" => $moreblah);
    } else {
        $newArray[$a1] = array("blah" => 0, "moreblah" => 0);
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

When I try add this to my application it doesn't see to work for some reason. The blah values are always 0 even when they are present in the second array, but I will mark it as answer because I think it is an issue with the rest of my code because I have tried it on writecodeonline and it works fine. Thanks
I tested this myself, so it should work fine, but as I edited, this code is only good if u know the keys in the second array..
I do know the blah and moreblah keys but when I print out the $newarray and $array2 heres what I get: Array([a]=>Array([blah]=>0, [moreblah]=>0), [b]=>Array([blah]=>0, [moreblah]=>0), [c]=>Array([blah]=>0, [moreblah]=>0)) and for $array2 I get Array([c]=>Array([blah]=>10, [moreblah]=>12)) Any reason for this?
I have no idea, when I test the code i pasted here, I get the exact same thing you just pasted, so I guess your $array2 is different :P
Ok I think I have worked it out. For simplicity and to make the question more generalised I have used values like a and b but instead of that I am using a date which uses strtotime and I think that is the issue so now I am trying to store the array as a string and then hopefully it will work. Your function however works excellently. Thanks
0
$array1 = array("a", "b", "c");
$structured = array_fill_keys($array1, array('blah' => 0, 'moreblah' => 0));

Should give you the array you want as a base. Then it's a matter of looping over your data and adding them where needed.

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.