Home > OS >  PHP - array diff/ array intersect weird behaviour in Laravel 7 / PHP 7.3
PHP - array diff/ array intersect weird behaviour in Laravel 7 / PHP 7.3

Time:01-05

I'm trying to use array_intersect and array_diff to get common things and things that one user has that other don't

$userFinishedSeries = [1,3,4,5,6,7,9,10,11,15,16,17,24,28,29,30,35,36,37,38,42,44,45,46,48,52,55,57,58,61,304]

$otherUsersFinishedSeries = [[3, 6, 7, 12, 14, 15, 16, 28, 37, 41, 51, 57], [16, 28, 29, 36, 37, 38, 42], [3, 4, 14, 16, 24, 28, 29, 36, 37, 38, 39, 42, 55, 56, 57, 58, 61, 68], [31, 46, 48, 55, 61], [30, 31, 41, 52, 61], [14, 16, 28, 36, 37, 38, 42, 49, 50, 58, 63], [12, 37, 57], [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 24, 26, 27, 28, 29, 30, 31, 35, 36, 37, 38, 39, 40, 41, 42, 44, 45, 46, 47, 48, 49, 50, 51, 52, 54, 55, 56, 57, 58, 60, 61, 62, 68], [7, 14], [28, 41], [14, 16, 28, 38], [37], [3, 7, 14, 16, 28, 35, 36, 37, 38, 41, 42, 55, 56, 57, 58, 60, 61, 64], [3, 4, 14, 16, 17, 28, 29, 36, 37, 38, 42, 55, 56, 61], [15, 16, 24, 26, 28, 31, 35, 36, 37, 38, 42, 52, 55, 56, 57, 58], [5, 7, 10, 11, 12, 14, 16, 17, 26, 27, 28, 29, 30, 36, 37, 38, 42, 46, 49, 54, 55, 57, 58, 64], [3, 4, 7, 14, 16, 26, 28, 36, 37, 38, 42, 49, 50, 55, 56, 57, 58, 61], [16, 28, 36], [16, 28, 30, 36, 37, 38, 46, 58], [16, 55], [16, 28, 35, 36, 37, 38, 42, 57, 58, 68], [15, 16, 27, 28, 30, 36, 37, 38, 42, 46, 56, 58, 61], [3, 16, 28, 37], [3, 7, 14, 16, 28, 36, 37, 38, 46, 49, 55, 56], [16, 28, 36, 37, 38, 42], [14, 16, 17, 28, 36, 37, 38, 42, 58, 61], [10, 17, 28, 37, 38, 42], [5, 7, 9, 16, 26, 28, 29, 36, 37, 38, 46, 57, 68], [], [14, 15, 16, 28, 30, 36, 37, 38, 42, 55, 56, 58], [], [58], [57], [16], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []];

$commonSeries = [];
$userFinishedSeriesThatOtherUsersDidNot = [];

foreach (array_filter($otherUsersFinishedSeries) as $key => $otherUserFinishedSeries) {
    $commonSeries[] = array_intersect($otherUserFinishedSeries, $userFinishedSeries);
    $userFinishedSeriesThatOtherUsersDidNot[] = array_diff($otherUserFinishedSeries, $userFinishedSeries);
};

I'm getting data from database then converting it to collection by get()->pluck('id')->sort()->values()->toArray() (userFinishedSeries, otherUsersFinishedSeries)

This code should give me array of arrays but when im returning those two arrays I got response with mixed objects inside of array

 "user_finished_series": [
    {
        "0": 3,
        "1": 6,
        "2": 7,
        "5": 15,
        "6": 16,
        "7": 28,
        "8": 37,
        "11": 57
    },
    [
        16,
        28,
        29,
        36,
        37,
        38,
        42
    ],
    {
        "0": 3,
        "1": 4,
        "3": 16,
        "4": 24,
        "5": 28,
        "6": 29,
        "7": 36,
        "8": 37,
        "9": 38,
        "11": 42,
        "12": 55,
        "14": 57,
        "15": 58,
        "16": 61
    },
    {
        "1": 46,
        "2": 48,
        "3": 55,
        "4": 61
    },
    {
        "0": 30,
        "3": 52,
        "4": 61
    },
    {
        "1": 16,
        "2": 28,
        "3": 36,
        "4": 37,
        "5": 38,
        "6": 42,
        "9": 58
    },
    {
        "1": 37,
        "2": 57
    },
    {
        "0": 3,
        "1": 4,
        "2": 5,
        "3": 6,
        "4": 7,
        "6": 9,
        "7": 10,
        "8": 11,
        "11": 15,
        "12": 16,
        "13": 17,
        "14": 24,
        "17": 28,
        "18": 29,
        "19": 30,
        "21": 35,
        "22": 36,
        "23": 37,
        "24": 38,
        "28": 42,
        "29": 44,
        "30": 45,
        "31": 46,
        "33": 48,
        "37": 52,
        "39": 55,
        "41": 57,
        "42": 58,
        "44": 61
    },
    [
        7
    ],
    [
        28
    ]...

I tried something like

   foreach($commonSeries as $series){
        if(empty($series) || !is_array($series)){
            unset($commonSeries[$key]);
        }
    }

But it doesn't do the trick, any ideas why im getting random objects mixed in my arrays?

CodePudding user response:

Is that what you want :

$userFinishedSeries = [1, 3, 4, 5,]; 
$otherUsersFinishedSeries = [[3, 6, 7, 12, 14, 1], [1,4,16, 28, 29, 36]];

$commonSeries = [];
$userFinishedSeriesThatOtherUsersDidNot = [];

foreach (array_filter($otherUsersFinishedSeries) as $key => $otherUserFinishedSeries) {
    $commons = array_intersect($otherUserFinishedSeries, $userFinishedSeries);
    foreach ($commons as $element) {
        $commonSeries[] = $element;
    }
    
    $diffs = array_diff($otherUserFinishedSeries, $userFinishedSeries);
    foreach ($diffs as $element) {
        $userFinishedSeriesThatOtherUsersDidNot[] = $element;
    }
};

$commonSeries = array_unique($commonSeries);
$userFinishedSeriesThatOtherUsersDidNot = array_unique($userFinishedSeriesThatOtherUsersDidNot);

$result['commonSeries'] = $commonSeries;
$result['userFinishedSeriesThatOtherUsersDidNot'] = $userFinishedSeriesThatOtherUsersDidNot;

print_r($result);

CodePudding user response:

Thanks for help, I figured out it myself

    foreach($commonSeries as $key => $series){
        $commonSeries[$key] = array_values($series);
    }

    foreach($userFinishedSeriesThatOtherUsersDidNot as $key => $series){
        $userFinishedSeriesThatOtherUsersDidNot[$key] = array_values($series);
    }

Those two loops seems to eliminate my problem

  • Related