Search for multiple array values ​​and search result count

I have an array of array data from $ search and $ data strong>, like this:

$data = array(
    array(1, 2, 3),
    array(1, 2, 3, 4, 7, 13)
);

$search = array(
    array(1, 2),
    array(1, 3),
    array(6, 13),
    array(7, 13)
);

$result = array();

I want each pair (example: 1 and 2) of array data from $ search to match all the data inside the $ data strong> variable , it will calculate how much data matches (EXAMPLE: 1 and 2 have a match for $ data [0 ] and $ data [1] , so the result should be 2). And then save the result of the calculation in the variable $ result based on the key from $ search .

Here is the result I was looking for:

$result = array(
    0 => 2,
    1 => 2,
    2 => 0
    3 => 1
);

And this is my code:

for ($i=0; $i<count($search); $i++) {
    for ($j=0; $j<count($data); $j++) {
        if (count(array_intersect($search[$i], $data[$j])) > 1) {
            array_push($result[$i], "1");
        }
    }
}

, $result, : array_push() , 1 . array_push $search, :

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 1
            [3] => 1
        )

    [1] => Array
        (
            [0] => 1
            [1] => 3
            [2] => 1
            [3] => 1
        )

    [2] => Array
        (
            [0] => 6
            [1] => 13
        )

    [3] => Array
        (
            [0] => 7
            [1] => 13
            [2] => 1
        )

)

, , , . , .

+4
3

, $result[$i] , array_push . :

foreach($search as $index => $searchArray) {
    $result[$index] = 0;
    foreach($data as $dataArray) {
        if (count(array_intersect($searchArray, $dataArray)) > 1) {
            $result[$index]++;
        }
    }
}
+2
foreach ($search as $key => $value) {
    $count = 0;
    if(in_array($value[0], $data[0]) && in_array($value[1], $data[0])){
        $count += 1;
    }
    if(in_array($value[0], $data[1]) && in_array($value[1], $data[1])){
        $count += 1;
    }
    $result[$key] = $count;
}
+3

:

$result = array();
foreach($search as $k => $v){
    $result[$k] = array_count_values($data[0])[$v[0]] + array_count_values($data[1])[$v[1]];

}
print_r($result);
0

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


All Articles