Home > other >  PHP: Translation table to convert mutiple values to another value?
PHP: Translation table to convert mutiple values to another value?

Time:06-20

I have a a number of values/IDs that need to be translated to a single ID, what is the recommended method using PHP?

For example, I want IDs 38332, 84371, 37939, 1275 to all translate to ID 1234 and IDs222, 47391, 798 to all translate to ID 1235, etc. .

I'm thinking PHP has something built-in to handle this efficiently?

CodePudding user response:

I'm thinking PHP has something built-in to handle this efficiently?

You can use the standard array as a map, quickly translating one ID to another:

$table[38332]; # int(1234)

depending on how you store your overall translation table, you can create a function that returns the translation from its input:

$table = $translation('I want IDs 38332, 84371, 37939, 1275 to all translate to ID 1234');
$result = $table[1275] ?? null; # int(1234)

Example:

$parseId = static fn(string $i) => (int)trim($i);
$translation = static fn(string $buffer): array
    => preg_match_all('~((?:\d ,\s*) \d )\s to all translate to ID\s*(\d )~', $buffer, $_, PREG_SET_ORDER)
    ? array_reduce($_, static fn (array $carry, array $item): array => [
        $ids = array_map($parseId, explode(',', $item[1])),
        $carry  = array_fill_keys($ids, $parseId($item[2])),
        $carry,][2], []) : [];

CodePudding user response:

This is pretty easy to accomplish with PHP, here's one way you could do it:

Using this method, you populate the $map array, using the id you want to replace with as the key, and the value being an array of the keys you want to be replaced. It then calculates a simple key => value array based on this to make comparison a lot quicker.

Instead of creating a copy of the data, you could use foreach ($data as &$record)

$data = [
    [
        'id' => 1,
        'foreign_id' => 38332,
        'text' => 'a'
    ],
    [
        'id' => 2,
        'foreign_id' => 84371,
        'text' => 'b'
    ],
    [
        'id' => 3,
        'foreign_id' => 37939,
        'text' => 'c'
    ],
    [
        'id' => 4,
        'foreign_id' => 1275,
        'text' => 'd'
    ],
    [
        'id' => 5,
        'foreign_id' => 222,
        'text' => 'e'
    ],
    [
        'id' => 5,
        'foreign_id' => 47391,
        'text' => 'f'
    ],
    [
        'id' => 5,
        'foreign_id' => 798,
        'text' => 'g'
    ]
];

$map = [
    123 => [
        38332,
        84371,
        37939,
        1275
    ],
    1235 => [
        222,
        47391,
        798
    ]
];

// Calculate a map to speed things up later
$map_calc = [];
foreach ($map as $destination_id => $ids) {
    foreach ($ids as $id) {
        $map_calc[$id] = $destination_id;
    }
}

$new_data = [];
foreach ($data as $record) {
    if (isset($map_calc[$record['foreign_id']]))
        $record['foreign_id'] = $map_calc[$record['foreign_id']];

    $new_data[] = $record;
}

var_dump($new_data);
  •  Tags:  
  • php
  • Related