1

I have array

$langs = array('en', 'de', 'pt', .....);

and now I would like to generate an array like this:

$result = array(
  array('en' => array('de', 'pt'),
  array('de' => array('en', 'pt'),
  array('pt' => array('en', 'de'),
  ....
);

Thanks!

2 Answers 2

1

You can also get the result with array_diff and array_values

$langs = array("en", "de", "pt");        

foreach($langs as $val) 
{
        $array[$val] = array_values(array_diff($langs, array($val)));      
}

print_r($array);
Sign up to request clarification or add additional context in comments.

Comments

1

You can try this way:

 $langs = array('en', 'de', 'pt');
 $arr = array();
 foreach($langs as $key => $lang){
        $arr[$lang] = $langs;
        unset($arr[$lang][$key]);
        sort($arr[$lang]);
 }

 print_r($arr);

CHECK PHP FIDDLE

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.