Home > Blockchain >  Is there a better data structure in PHP other than array or SPL library for exact matched key <-&
Is there a better data structure in PHP other than array or SPL library for exact matched key <-&

Time:04-02

I am looking for a data structure that returns a matching value where when the input is one integer value it has only one exact integer match. For example if I put in 1 it returns 2 or if I put in 2 it returns 1. I have one solution already(See below). However, I feel like there has to be a data structure that has the format like this: 1 <=> 5, 3 <=> 6, etc. I don't have years of PHP knowledge so I am not sure if I am just unaware of a native data structure that does this. I also realize that this may not exist, I just want to make sure that it doesn't.

As of right now I just have an array with key value pairs for each corresponding value. (I also did the same with a switch statement but it looked less clean). It looks like this:

function vs_get_matching_number($number){
      $matching_numbers = array(
       1 => 3,
       3 => 1,
       4 => 6,
       6 => 4
     );
     return $matching_numbers[(int)$number];
   }

CodePudding user response:

Array is indeed the best choice, despite its name in reality it is a Hashmap, which is what you actually want. It's a PHP-native type, useable by any other code without dependencies (eg. no need for special extension or additional code).

Based on a "source" array with only one-way mapping, you can do it like this:

function array_flip_merge($arr) {
    $result = $arr;
    $flip = array_flip($arr);
    foreach($flip as $k => $v) {
        $result[ $k ] = $v;
    }
    return $result;
}

$source = [
   1 => 3,
   4 => 6,
];

$result = array_flip_merge($source);

array_flip() will create a second array with keys and values exchanged. This inserted into the original array will produce the desired output array.

Array
(
    [1] => 3
    [4] => 6
    [3] => 1
    [6] => 4
)

Note that there are no checks whatsoever in the function, so it won't notice inconsistencies in the source array (f.e. if one of the keys or values is duplicate). But it could be implemented as well...

CodePudding user response:

It looks like you're trying to map numbers in either direction, you could do something like this. It still uses an array but it reduces the duplication.

  • array_search() can be used to find the key based on value
  • isset() can be used to check a key exists before returning the value

An exception could be thrown if a number is not found, this could be handled via a try catch statement.

<?php
function vs_get_matching_number($number) {
    $matchingNumbers = [
            1 =>3,
            4 =>6,
        ];
        
    if(isset($matchingNumbers[$number])) {
        return $matchingNumbers[$number];
    }
   
    $keyNumber = array_search($number, $matchingNumbers);
    if ($keyNumber) {
        return $keyNumber;
    }
   
    throw new \Exception('Unknown number: '.$number);
   
}

echo vs_get_matching_number(1) ."\n";
echo vs_get_matching_number(3) ."\n";
echo vs_get_matching_number(4) ."\n";
echo vs_get_matching_number(6) ."\n";
echo vs_get_matching_number(5) ."\n";
  • Related