Verwijder dubbels uit array
Ik heb volgende JSON:
Code (php)
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
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
{
"player": "yingyu",
"cards": [
{
"player": "yingyu",
"uid": "C3-248-9G9SA73GZK",
"card_detail_id": 248,
"xp": 1,
"gold": false,
"edition": 3,
"level": 1
},
{
"player": "yingyu",
"uid": "C3-283-MNVC8ET79S",
"card_detail_id": 283,
"xp": 1,
"gold": false,
"edition": 3,
"level": 1
},
{
"player": "yingyu",
"uid": "C3-283-2SIKI2BX9S",
"card_detail_id": 283,
"xp": 4,
"gold": false,
"edition": 3,
"level": 2
}
]
}
"player": "yingyu",
"cards": [
{
"player": "yingyu",
"uid": "C3-248-9G9SA73GZK",
"card_detail_id": 248,
"xp": 1,
"gold": false,
"edition": 3,
"level": 1
},
{
"player": "yingyu",
"uid": "C3-283-MNVC8ET79S",
"card_detail_id": 283,
"xp": 1,
"gold": false,
"edition": 3,
"level": 1
},
{
"player": "yingyu",
"uid": "C3-283-2SIKI2BX9S",
"card_detail_id": 283,
"xp": 4,
"gold": false,
"edition": 3,
"level": 2
}
]
}
Dit is de JSON data voor 3 kaarten. Echter, kaart 2 en 3 zijn eignelijk dezelfde kaart (card_detail_id is voor beide kaarten hetzelfde). Met kaart 2 wil ik dus eigenlijk niets doen.
Momenteel gebruik ik een foreach loop om door elke kaart te gaan en op basis van de waardes in de JSON genereer ik de artwork automatisch. Ik wil dat wanneer meerdere kaarten dezelfde card_detail_id hebben, enkel de kaart getoond wordt die het hoogste level heeft. Hoe zou ik zoiets aanpakken?
Dit is de PHP code die ik momenteel heb:
Code (php)
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
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
<?php
$contents2 = file_get_contents("https://api.splinterlands.io/cards/get_details");
$array2 = json_decode($contents2, true);
$contents3 = file_get_contents('https://api2.splinterlands.com/cards/collection/yingyu');
$array3 = json_decode($contents3, true);
foreach ($array3['cards'] as $card) {
$colcardid = $card['card_detail_id'];
$colcardid--;
$colcardname = $array2[$colcardid]['name'];
$colcardlvl = $card['level'];
$colcardeditionnr = $card['edition'];
if ($colcardeditionnr == "0,1") {
$colcardeditionname = "beta";
} elseif ($colcardeditionnr == "1") {
$colcardeditionname = "beta";
} elseif ($colcardeditionnr == "2") {
$colcardeditionname = "promo";
} elseif ($colcardeditionnr == "3") {
$colcardeditionname = "reward";
} elseif ($colcardeditionnr == "4") {
$colcardeditionname = "untamed";
} elseif ($$colcardeditionnr == "5") {
$colcardeditionname = "dice";
}
$colcardgold = $card['gold'];
if ($colcardgold == true) {
$colcardgoldstring = "_gold";
} else {
$colcardgoldstring = "";
}
echo '<img class="card" src="https://d36mxiodymuqjm.cloudfront.net/cards_by_level/' . $colcardeditionname . '/' . $colcardname . '_lv' . $colcardlvl . $colcardgoldstring . '.png" height="100" alt="' . $colcardname . '" />';
}
?>
$contents2 = file_get_contents("https://api.splinterlands.io/cards/get_details");
$array2 = json_decode($contents2, true);
$contents3 = file_get_contents('https://api2.splinterlands.com/cards/collection/yingyu');
$array3 = json_decode($contents3, true);
foreach ($array3['cards'] as $card) {
$colcardid = $card['card_detail_id'];
$colcardid--;
$colcardname = $array2[$colcardid]['name'];
$colcardlvl = $card['level'];
$colcardeditionnr = $card['edition'];
if ($colcardeditionnr == "0,1") {
$colcardeditionname = "beta";
} elseif ($colcardeditionnr == "1") {
$colcardeditionname = "beta";
} elseif ($colcardeditionnr == "2") {
$colcardeditionname = "promo";
} elseif ($colcardeditionnr == "3") {
$colcardeditionname = "reward";
} elseif ($colcardeditionnr == "4") {
$colcardeditionname = "untamed";
} elseif ($$colcardeditionnr == "5") {
$colcardeditionname = "dice";
}
$colcardgold = $card['gold'];
if ($colcardgold == true) {
$colcardgoldstring = "_gold";
} else {
$colcardgoldstring = "";
}
echo '<img class="card" src="https://d36mxiodymuqjm.cloudfront.net/cards_by_level/' . $colcardeditionname . '/' . $colcardname . '_lv' . $colcardlvl . $colcardgoldstring . '.png" height="100" alt="' . $colcardname . '" />';
}
?>
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
$cards = $array['cards'];
$ids = array_unique(array_column($cards,'card_detail_id')); //unieke card_detail_id's
foreach($ids as $id){ //voor ieder uniek ID de hoogste kaart bepalen
//het hoogste level bepalen
$level = max(array_column($selection = array_filter($cards,function($card) use ($id){
return $card['card_detail_id'] == $id;
}),'level'));
//de (eerste) kaart met dit level tonen
foreach($selection as $card) if($card['level'] == $level){
print_r($card); //toon kaart
break;
}
}
?>
$cards = $array['cards'];
$ids = array_unique(array_column($cards,'card_detail_id')); //unieke card_detail_id's
foreach($ids as $id){ //voor ieder uniek ID de hoogste kaart bepalen
//het hoogste level bepalen
$level = max(array_column($selection = array_filter($cards,function($card) use ($id){
return $card['card_detail_id'] == $id;
}),'level'));
//de (eerste) kaart met dit level tonen
foreach($selection as $card) if($card['level'] == $level){
print_r($card); //toon kaart
break;
}
}
?>
Dus als die api's in jouw beheer zijn, zou ik daar al de dubbelen tegen proberen te houden. (en dan ook vast nadenken of je altijd de 1e wilt houden, of dat daar soms uitzonderingen op zijn.