-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp46.php
More file actions
39 lines (30 loc) · 904 Bytes
/
php46.php
File metadata and controls
39 lines (30 loc) · 904 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
class User
{
public $name;
public $hobbies;
}
function countBestHobbies($usersArray)
{
$hobbiesArray = [];
foreach ($usersArray as $user) {
foreach ($user->hobbies as $hobbie) {
array_key_exists($hobbie, $hobbiesArray) ? $hobbiesArray[$hobbie] += 1 : $hobbiesArray[$hobbie] = 1;
}
}
print_r($hobbiesArray);
}
$user1 = new User();
$user1->name = "Cesar";
$user1->hobbies = ["Code", "Travel", "Basket", "Videogames"];
$user2 = new User();
$user2->name = "Alan";
$user2->hobbies = ["Code", "Travel", "Basket"];
$user3 = new User();
$user3->name = "Laura";
$user3->hobbies = ["Code", "Basket", "Videogames"];
$user4 = new User();
$user4->name = "Martha";
$user4->hobbies = ["Code", "Concerts", "Videogames"];
countBestHobbies([$user1, $user2, $user3, $user4]);
/* Return how many students had the same hobbie with the given students array */