Create an array from a long list of LCIDs

49 views Asked by At

I have a long list of LCIDs which I want to assign to each other. Here is an extract:

1031
1033
1034
1061
1062
1026

I want to end up having an array or a list which looks like this:

1031 => 1033
1031 => 1034
1031 => 1061
1031 => 1062
1031 => 1026
1033 => 1031
1033 => 1034
1033 => 1061
1033 => 1062
1033 => 1026
1034 => 1031
1034 => 1033
1034 => 1061
1034 => 1062
1034 => 1026

And so on.

Any advice on how to tackle this in PHP? Help is much aprreciated!

EDIT: I now came up with this (still not what I need but I'm getting there somehow...)

foreach($a_languageIDs as $lcid){

    $src_lcid = $lcid + 1024;
    $trg_lcid = $lcid + 1024;

        for ($i = 0; $i < count($a_languageIDs); $i++) {

            echo "$src_lcid -> $trg_lcid\r\n";
        }
}

Which gives me this output:

1031 -> 1031
1031 -> 1031
1031 -> 1031
1031 -> 1031
1031 -> 1031
1031 -> 1031
1031 -> 1031
1031 -> 1031
1031 -> 1031
1031 -> 1031
1031 -> 1031
1031 -> 1031
1031 -> 1031
1031 -> 1031
1031 -> 1031
1031 -> 1031
1031 -> 1031
1031 -> 1031
1031 -> 1031
1031 -> 1031
1031 -> 1031
1031 -> 1031
1031 -> 1031
1031 -> 1031
1031 -> 1031
1031 -> 1031
1031 -> 1031
1033 -> 1033
1033 -> 1033
1033 -> 1033
1033 -> 1033
1033 -> 1033
1033 -> 1033
1033 -> 1033
1033 -> 1033
1033 -> 1033
1033 -> 1033
1033 -> 1033
1033 -> 1033
1033 -> 1033
1033 -> 1033
1033 -> 1033
1033 -> 1033
1033 -> 1033
1033 -> 1033
1033 -> 1033
1033 -> 1033
1033 -> 1033
1033 -> 1033
1033 -> 1033
1033 -> 1033
1033 -> 1033
1033 -> 1033
1033 -> 1033

This at least gives me this iterated list but I still have no idea how to adapt the value of $trg_lcid.

1

There are 1 answers

1
El_Vanja On

You can utilize array_diff within a loop:

$lcids = [1031, 1033, 1034, 1061, 1062, 1026];
$allCombinations = [];
foreach ($lcids as $lcid) {
    $allOtherLCIDs = array_diff($lcids, [$lcid]);
    foreach ($allOtherLCIDs as $otherLCID) {
        echo "$lcid -> $otherLCID<br>";
    }
}

How this works:

  • We iterate your main array, to find combinations for each of the elements.
  • We use array_diff to create an array that holds all the elements except the current one. It works by comparing the first parameter (the full array) against the second one (an array holding only the current element) and returning all the elements of the first parameter that are not present in the second parameter.
  • After that we iterate the created diff and output the current element of the outer loop (main array) for every element of the diff array.