1

I have two array. I want to merge then based on their key match. Here is my first array

array(
  (int) 1 => 'Home',
  (int) 7 => 'FAQs',
  (int) 16 => 'Roles',
  (int) 21 => 'Terms & Condition',
  (int) 24 => 'Registration',
  (int) 4 => 'Posts'
)

My second array

array(
   (int) 4 => array(
            (int) 0 => 'All Posts',
            (int) 1 => 'Post Add'
   ),
   (int) 7 => array(
            (int) 0 => 'FAQ Add',
            (int) 1 => 'All FAQs'
   ),
   (int) 16 => array(
            (int) 0 => 'Add Role',
            (int) 1 => 'All Role & Actions'
   ),
   (int) 21 => array(
              (int) 0 => 'Add Terms & Condition',
              (int) 1 => 'Terms & Conditions'
    )
 )

What I want to do is to create a new array with their values if key matches.like this way. Is this even possible to merge them like this ?

 array(
   'Posts' => array(
            (int) 0 => 'All Posts',
            (int) 1 => 'Post Add'
   ),
   'FAQs' => array(
            (int) 0 => 'FAQ Add',
            (int) 1 => 'All FAQs'
   ),
   'Registration' => array(),
   'Home'=>array(),
   'Roles' => array(
            (int) 0 => 'Add Role',
            (int) 1 => 'All Role & Actions'
   ),
   'Terms & Condition' => array(
              (int) 0 => 'Add Terms & Condition',
              (int) 1 => 'Terms & Conditions'
    )
 )
5
  • 3
    I think there's an error in the question, or its not well explained. For example first array has keys 1 and 13 which don't appear in 2nd array but result has a key related to 1=>home but not a key related to 13=>menu&actions. Why? Also second array has key 10 with 4 items but result has 2 of those but not the other two. Why? Also "a new array with their values if key matches like this way", exactly which keys match with which, in what way? Can you recheck the question to make sure no errors and explain why the other 2 answers are not okay? Commented Apr 3, 2016 at 14:23
  • To receive correct answer you have to reply to @Stilez comment, and also to explain the sort criteria, because it appears based on second array with random insertion of first array elements. Commented Apr 4, 2016 at 1:38
  • @stilez - Sorry my bad. Thank you for pointing that out. 1. I wanted to say if key not available in second array then return an array matches with 2nd array and empty arrays from first array. 2. 13 no key from first array should be 10 which is a mistake while data entry. 3. first answer partially works. it doesn't return empty array if key doesn't match with second array. Commented Apr 4, 2016 at 4:49
  • @fusion3k - thank you for reminding that but i was out for a long time ,just came back Commented Apr 4, 2016 at 4:50
  • Can you use the edit button to fix the question then, so other people can see the correct arrays Commented Apr 4, 2016 at 12:55

3 Answers 3

2

try

<?php
$new_array = array();
foreach($first_array as $key=>$value){
    if(array_key_exists($key, $second_array)){  
     $new_aray[$value] = $arr2[$key];   
    }
}
print_r($new_array);

hope it helps :)

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

2 Comments

if key doesn't match then return noting so I used else condition which provides the desired output but an error which is undefined offset 1,24
It works as desired. just added else with else{ $new_array[$value] = array() ; }
1

Try this loop out:

$thirdArray = array();

foreach($firstArray as $fk => $fv){
    if(isset($secondArray[$fk])){
        $thirdArray[$fv] = $secondArray[$fk];
    }
}

var_dump($thirdArray);

Comments

1

If you want to use first array as base (maintaining all values as keys) you can try this solution with array_map and array_flip ($menus is your first array, $submenus is the second array):

$menu = array_map
(
    function( $key ) use( $submenus )
    {
        return (isset($submenus[$key])) ? $submenus[$key] : array();
    },
    array_flip( $menus )
);

$menu array now contains:

Array
(
    [Home] => Array
        (
        )
    [FAQs] => Array
        (
            [0] => FAQ Add
            [1] => All FAQs
        )
    [Menu & Actions] => Array
        (
        )
    [Roles] => Array
        (
            [0] => Add Role
            [1] => All Role & Actions
        )
    [Terms & Condition] => Array
        (
            [0] => Add Terms & Condition
            [1] => Terms & Conditions
        )
    [Registration] => Array
        (
        )
    [Posts] => Array
        (
            [0] => All Posts
            [1] => Post Add
        )
)

eval.in demo

We use array_map to map flipped $menus array (array_flip inverts keys and values): in the anonymous function used inside array_map, we search for the given $menus value (originally key) in $submenus keys: if the key exists, we return it, otherwise we return an empty 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.