Wednesday 15 February 2012

php - Check if array elements are in a different array -


I am trying to create a function to correct if all items in $ array1 are in $ array2, if incorrect Does not return.

What am I trying to do:

  $ array1 = ['1', '3', '9', '17']; $ Array2 = ['1', '2', '3', '4', '9', '11', '12', '17']; Function check returned ($ arr, $ arr2) {if all elements of $ arr are returned in $ arr2 returns, then return true) if (checker ($ array1, $ array2) {// function returned true} Other {/ / Function returned incorrectly}   

I can not think of how to do it! Very much appreciated.

Solution:

  Function Checker ($ Needle, $ Harestack) {$ x = array_diff ($ needles, $ hansstack); if (count ($ x)> 1) {return false;} other {return;}} // false False because more needles The heap of grass taken from the check array ([1,2,3,4,5], [1,2,3]) returns the // returns because all the needles are stacked with grass stacks ([1,2, 3], [1, 2,3,4,5]);    

 < Code> function checker ($ Arr, $ arr2) {return! Count (array_diff ($ arr, $ arr2))}}   

array_diff : all entries $ arr that are not present in $ arr2 .

No comments:

Post a Comment