Array search for similar objects

For an array of arrays like this:

$array = array( 
            0 => array (
               0 => 35,
               1 => 30, 
               2 => 39
            ),
            1 => array (
               0 => 20,
               1 => 12, 
               2 => 5
            ),
            ...

            n => array (
               0 => 10,
               1 => 15, 
               2 => 7
            ),
         );

I need to find an entry in an array that is closer to the given parameters

find($a, $b, $c) {
  //return the closer entry to the input
}

For closer input, I mean a record that has closer values ​​to the values ​​specified in the input, for example. (19, 13, 3) it should return $ array [1]

The way I am doing the calculation at the moment is a loop over the entire array, storing the variable $ distance, starting at -1, and the temporary variable $ result. For each element, I calculate the distance

$dist = abs( subarray[0] - $a ) + abs ( subarray[1] - $b ) + abs( subarray[2] - $c ) 

and if the calculated distance is -1 or lower than the $ distance variable, which is outside the loop, I assign a new distance to varaible and save the corresponding array in the $ result variable. At the end of the loop, I get the desired value.

, : , (19, 13, false) $array [1], -

$dist = abs( subarray[0] - $a ) + abs ( subarray[1] - $b );

[2] $c.

, , , . , . , ?

+4
2

( ) . , .

PS: , . , .

$array = array(
    0 => array (
        0 => 35,
        1 => 30,
        2 => 39
    ),
    1 => array (
        0 => 20,
        1 => 12,
        2 => 5
    ),
);

$user = array(19,13,3);

function find($referencial, $input){
    $totalRef = count($referencial);
    if (is_array($referencial)){
        for ($i = 0; $i < $totalRef; $i++) {
            if (is_array($referencial[$i])){
                $totalSubRef = count($referencial[$i]);
                $proximity = array();
                for ($j = 0; $j < $totalSubRef; $j++) {
                    $proximity[$i] += abs($referencial[$i][$j] - $input[$j]);
                }
                if ($i > 0){
                    if ($maxProximity['distance'] > $proximity[$i]) {
                        $maxProximity['distance'] = $proximity[$i];
                        $maxProximity['index'] = $i;
                    }
                } else {
                    $maxProximity['distance'] = $proximity[$i];
                    $maxProximity['index'] = $i;
                }
            }
        }
        return $maxProximity;
    } else {
        exit('Unexpected referencial. Must be an array.');
    }
}

$found = find($array, $user);
print_r($found);
//Array ( [distance] => 4 [index] => 1 )
print_r($array[$found['index']]);
// Array ( [0] => 20 [1] => 12 [2] => 5 )
+1

- , , :

:

($ mArray [0... 3]) , ($ mNumbersToFind [0... 3]. ( ) - max - .

$array = array( 
            array (
               0 => 13,
               1 => 15, 
               2 => 4
            ),
            array (
               0 => 20,
               1 => 12, 
               2 => 5
            ),

            array (
               0 => 13,
               1 => 3, 
               2 => 15
            ),
         );


$mNumbersToFind = array(13,3,3);

$mFoundArray = find($mNumbersToFind, $array);

echo "mFinalArray : <pre>";
print_r($mFoundArray);


function find($mNumbersToFind, $mArray){

    $mPossibilityMax = count($mNumbersToFind);
    $mBiggestPossibilityElementPosition = 0;
    $mBiggestPossibilityUntilNow = 0;


    foreach($mArray as $index => $current){

        $maxPossibility = 0;

        foreach($current as $subindex => $subcurrent){



            $mTempArray[$index][$subindex]['value'] = $subcurrent - $mNumbersToFind[$subindex];

            $percentChange = (1 - $mTempArray[$index][$subindex]['value'] / $subcurrent) * 100;
            $mTempArray[$index][$subindex]['possibility'] = $percentChange;
            $maxPossibility += $percentChange/$mPossibilityMax;


        }


        $mTempArray[$index]['final_possibility'] = $maxPossibility;

        if($maxPossibility > $mBiggestPossibilityUntilNow){
            $mBiggestPossibilityUntilNow = $maxPossibility;
            $mBiggestPossibilityElementPosition = $index;
        }



    }

    echo "mTempArray : <pre>"; // Remove this - it just for debug
    print_r($mTempArray); // Remove this - it just for debug

    return $mArray[$mBiggestPossibilityElementPosition];
}

($ mTempArray):

mTempArray :
Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [value] => 0
                    [possibility] => 100
                )

            [1] => Array
                (
                    [value] => 12
                    [possibility] => 20
                )

            [2] => Array
                (
                    [value] => 1
                    [possibility] => 75
                )

            [final_possibility] => 65
        )

    [1] => Array
        (
            [0] => Array
                (
                    [value] => 7
                    [possibility] => 65
                )

            [1] => Array
                (
                    [value] => 9
                    [possibility] => 25
                )

            [2] => Array
                (
                    [value] => 2
                    [possibility] => 60
                )

            [final_possibility] => 50
        )

    [2] => Array
        (
            [0] => Array
                (
                    [value] => 0
                    [possibility] => 100
                )

            [1] => Array
                (
                    [value] => 0
                    [possibility] => 100
                )

            [2] => Array
                (
                    [value] => 12
                    [possibility] => 20
                )

            [final_possibility] => 73.333333333333
        )

)

:

mFinalArray : 
Array
(
    [0] => 13
    [1] => 3
    [2] => 15
)
+1
source

Source: https://habr.com/ru/post/1677850/


All Articles