John Davidson

php - How to use array_intersect in one array

0 comments
Message:


I have this array and i want to compare each key items with each other and get similars with array_intersect . I wrote this code but it just compare first 2 key items and i want to compare all of key items.



            $res_arr = array_shift($m);
foreach($m as $filter){
$arr = array_intersect($res_arr, $filter);
}


My Answer:
To compare all key items in the array and get the similarities using `array_intersect`, you can modify your code as follows:

php
$res_arr = array_shift($m);
foreach($m as $filter){
$res_arr = array_intersect($res_arr, $filter);
}

print_r($res_arr);


This code will compare each key item in the array with the previous result array `$res_arr` and update `$res_arr` with the similarities. This way, you will get the similarities between all key items in the array.

Rate this post

3 of 5 based on 7158 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us