0

I have two arrays. One has group names the other one has group items. I want to assign group names as keys to the second array.

Example:

$array1 = array(
  0 => "A",
  1 => "B"
);

$array2 = array(
  0 => "a,b,c,d",
  1 => "e,f,g,h"
);

The second array should become:

$array3 = array(
  A => "a,b,c,d",
  B => "e,f,g,h"
);

How can i achieve this in PHP?

Thanks

0

3 Answers 3

4

use array_combine as such :

$array2 = array_combine($array1, $array2);
Sign up to request clarification or add additional context in comments.

Comments

2

you need to use array_combine, api here

Comments

0

would work like this:

    <?php 
    $grpNames = array(0 => "A", 1 => "B");
    $grpItems = array(0 => "a,b,c,d", 1 => "e,f,g,h");
    $newArray = array();
        foreach($grpItems as $grpItemKey => $grpItems){
            if(isset($grpNames[$grpItemKey])){
                $newArray[$grpNames[$grpItemKey]] = $grpItems;
            }
        }

    var_dump($newArray);
     ?>

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.