Quick actions

cmd+k|ctrl+k

Navigation

Languages

array operation example

Snippet info

Language

Php

Visibility

public

Author

kykurniawan

Created

2022-09-22T11:39:15.79963Z

Updated

2022-09-22T11:40:18.63Z

<?php 


$basketballlLovers = ["ARDI", "RESKA", "SISKA"];
$soccerLovers = ["SISKA", "BUDI", "DAMIAN"];
$swimmingLovers = ["DAMIAN", "RESKA", "ANITA"];

function getStudentWhoLoveBasketballAndSoccer(array $basketballlLovers, array $soccerLovers): array
{
    return array_intersect($basketballlLovers, $soccerLovers);
}

function getStudentWhoLoveBasketballOrSwimming(array $basketballlLovers, array $swimmingLovers): array
{
    return array_unique(array_merge($basketballlLovers, $swimmingLovers));
}

function getStudentWhoOnlyLoveSoccer(array $basketballlLovers, array $soccerLovers, array $swimmingLovers): array
{
    $compareWithBasketball = array_diff($soccerLovers, $basketballlLovers);
    $compareWithSwimming = array_diff($soccerLovers, $swimmingLovers);
    
    return array_intersect($compareWithBasketball, $compareWithSwimming);
}

// love basketball and soccer
print_r(getStudentWhoLoveBasketballAndSoccer($basketballlLovers, $soccerLovers));

// love basketball or swimming
print_r(getStudentWhoLoveBasketballOrSwimming($basketballlLovers, $swimmingLovers));

// love soccer only
print_r(getStudentWhoOnlyLoveSoccer($basketballlLovers, $soccerLovers, $swimmingLovers));
INFO