1

How do I find the keys array of disciplines that have appropriate values?
For Example:

$arr1 = [2, 4, 12];
$result = [...] // Var_dump in link   
in_array($arr1, $result);

Regardless of their order, I need to see if there is a set of keys or a no. But in_array() does not work. Thanks!

Dump array


Update (01.03.2017)

This is my version of the solution to this problem

 $findResult = array_filter($result, function($val)use($get){
            $requiredDisciplines = [1, $get['disciplines']['second'], $get['disciplines']['third'], $get['disciplines']['four']]; // запрос
            $activePriorities = [];

            foreach ($val['disciplines'] as $discipline) {
                if (in_array($discipline['id'], $requiredDisciplines)) {
                    $activePriorities[] = $discipline['priority'];
                }
            }

            for ($i = 0; $i<3; $i++){
                if(!in_array($i, $activePriorities))
                    return false;
            }

            /*if(in_array(0, $activePriorities) && in_array(1, $activePriorities) && in_array(2, $activePriorities) != true)
                return false;*/

          //  print_r($activePriorities);

            return true;
        });
ladone
  • 102
  • 9

1 Answers1

2

I've got a versatile one-liner that will give you all of the arrays containing matches. (so you can derive the keys or the count from that). (demo)

This function is only set to compare the values between the needle and the haystack, but can be set to search keys-values pairs by replacing array_intersect with array_intersect_assoc and adding ,ARRAY_FILTER_USE_BOTH to the end of the filter function (reference: 2nd snippet @ https://stackoverflow.com/a/42477435/2943403)

<?php
// haystack array
$result[]=array(1,2,3,4,5,6,7,8,9,10,11,12);
$result[]=array(1,3,5,7,9,11);
$result[]=array(2,4,6,8,10,12);

// needle array
$arr1=array(2,4,12);

//one-liner:
$qualifying_array=array_filter($result,function($val)use($arr1){if(count(array_intersect($val,$arr1))==count($arr1)){return $val;}});

/*
// multi-liner of same function:
$qualifying_array=array_filter(
    $result,
    function($val)use($arr1){  // pass in array to search
        if(count(array_intersect($val,$arr1))==count($arr1)){  // total pairs found = total pairs sought
            return $val;
        }
    }
);*/


echo 'Total sub-arrays which contain the search array($arr1): ',sizeof($qualifying_array),"<br>";
echo 'Keys of sub-arrays which contain the search array($arr1): ',implode(',',array_keys($qualifying_array)),"<br>";
echo 'Is search array($arr1) in the $result array?: ',(sizeof($qualifying_array)?"True":"False"),"<br>";
echo "<pre>";
print_r($qualifying_array);
echo "</pre>";

The output:

Total sub-arrays which contain the search array($arr1): 2
Keys of sub-arrays which contain the search array($arr1): 0,2
Is search array($arr1) in the $result array?: True
Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 6
            [6] => 7
            [7] => 8
            [8] => 9
            [9] => 10
            [10] => 11
            [11] => 12
        )

    [2] => Array
        (
            [0] => 2
            [1] => 4
            [2] => 6
            [3] => 8
            [4] => 10
            [5] => 12
        )

)
Community
  • 1
  • 1
mickmackusa
  • 37,596
  • 11
  • 75
  • 105
  • It looks like a solution to my problem. But, I found myself the answer to your question! Thank you for the answer! – ladone Mar 01 '17 at 18:14