The array may look like this, and the number of keys in the next internal array may be different. At the end of checking each value, the array should look the same. It is worth considering that the value may be textual.

Array ( [0] => Array ( [year] => 2008 [faculty] => 4 [program] => 10 [...] => ... ) [1] => Array ( [agree] => on [year] => 2010 [faculty] => 47 [program] => 4 [...] => ... ) ... ) 

It is not necessary to write the code, it is enough to tell in words or, as a last resort, add a pseudocode to the story.

Why do I need it? It’s just interesting) I ran into it myself, I didn’t come up with a similar array of a better solution than how to go through a loop with conditional constructions ...

  • 2
    And what is the test? - ling
  • array_merge ( array_intersect , array_diff , etc)? - Ilya Pirogov
  • Yes, perhaps the question is not correctly posed. The check can be, both on the fact that this data is at all under a certain array key (i.e. mandatory), as well as on the data type and by regular expressions. All values ​​are not checked equally. - uvlad

2 answers 2

The in_array() function checks whether the specified element is present in the array. If present, returns true , otherwise false . Function syntax:

 bool in_array(mexed элемент, array массив) 

array_keys() this function returns an array containing all the keys of the source array passed as a parameter. If the additional parameter "required element" is set during the call, only the keys that correspond to the specified value are returned; otherwise, all array keys are returned. Function syntax:

 array array_keys(array массив, [, mixed искомый_элемент]) 

array_values() function returns an array consisting of all the values ​​of the original array, passed as a parameter. Function syntax:

 array array_values(array массив) 

You can do a search through these functions. Create an array of items to check. and in the loop, search by these functions for each element or other search functions in the array of elements contained in the array)

    And I would use array_filter() with callback .

    If $array is the original array, then

     $filtered = array_filter($array, function($elem) { [здесь проверять все условия к конкретному подмассиву если условия выполняются - return true; если не выполняются - return false; тут уже ограничений нет. Например: if ($elem["agree"]!="on") return false; if (!array_key_exists("whatever", $elem)) return false; if ($elem["year"]>date('Y')) return false; вобщем все, что угодно.... }); 

    As a result, $filtered will be only those records in which the test callback returned true .