如何在symfony中对卡片进行排序
how to sort card in symfony
我是 symfony
的初学者,我想根据以下顺序(使用 SYMFONY
3)对 10 cards
(随机给用户)进行排序:
类别顺序:DIAMOND – HEART – SPADES – CLUB。
值顺序:AS – 2 – 3 – 4 – 5 – 6 – 7 – 8 – 9 – 10 – JACK – QUEEN – KING。
例如:我通过 JSON 文件恢复了用户的 10 张随机卡片:
{
"cards":[
{
"category":"DIAMOND",
"value":"TEN"
},
{
"category":"CLUB",
"value":"ACE"
},
{
"category":"DIAMOND",
"value":"QUEEN"
},
{
"category":"SPADES",
"value":"SEVEN"
},
{
"category":"DIAMOND",
"value":"NINE"
},
{
"category":"HEART",
"value":"QUEEN"
},
{
"category":"CLUB",
"value":"TEN"
},
{
"category":"HEART",
"value":"FIVE"
},
{
"category":"HEART",
"value":"SEVEN"
}
]
}
排序的牌变成:ACE CLUB /FIVE HEART / SIX CLUB / SEVEN HEART / SEVEN SPADES / NINE DIAMOND / TEN DAIMOND /TEN CLUB / QUEEN DIAMOND / QUEEN HEART
我不知道如何开始?
在这种情况下,您可以使用 usort()
然后应用您的逻辑。在这种情况下,我有两个类别和值数组,以及它们的顺序。在下方,您可以看到 usort
函数,它通过两个数组的索引比较值。你可以把函数变小,但我这样写是为了更好地理解:
<?php
$category_order = ["CLUB", "SPADES", "HEART", "DIAMOND"];
$value_order = ["ACE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING"];
$input = '{
"cards":[
{
"category":"DIAMOND",
"value":"TEN"
},
{
"category":"CLUB",
"value":"ACE"
},
{
"category":"DIAMOND",
"value":"QUEEN"
},
{
"category":"SPADES",
"value":"SEVEN"
},
{
"category":"DIAMOND",
"value":"NINE"
},
{
"category":"HEART",
"value":"QUEEN"
},
{
"category":"CLUB",
"value":"TEN"
},
{
"category":"HEART",
"value":"FIVE"
},
{
"category":"HEART",
"value":"SEVEN"
}
]
}';
$input = json_decode($input, true);
$cards = $input["cards"];
usort($cards, function($a, $b) use ($category_order, $value_order) {
if (array_search($a['category'], $category_order) > array_search($b['category'], $category_order)) {
return 1;
} elseif (array_search($a['category'], $category_order) == array_search($b['category'], $category_order)) {
if(array_search($a['value'], $value_order) > array_search($b['value'], $value_order)) {
return 1;
} else {
return -1;
}
} else {
return -1;
}
});
print_r($cards);
输出:
Array
(
[0] => Array
(
[category] => CLUB
[value] => ACE
)
[1] => Array
(
[category] => CLUB
[value] => TEN
)
[2] => Array
(
[category] => SPADES
[value] => SEVEN
)
[3] => Array
(
[category] => HEART
[value] => FIVE
)
[4] => Array
(
[category] => HEART
[value] => SEVEN
)
[5] => Array
(
[category] => HEART
[value] => QUEEN
)
[6] => Array
(
[category] => DIAMOND
[value] => NINE
)
[7] => Array
(
[category] => DIAMOND
[value] => TEN
)
[8] => Array
(
[category] => DIAMOND
[value] => QUEEN
)
)
我是 symfony
的初学者,我想根据以下顺序(使用 SYMFONY
3)对 10 cards
(随机给用户)进行排序:
类别顺序:DIAMOND – HEART – SPADES – CLUB。
值顺序:AS – 2 – 3 – 4 – 5 – 6 – 7 – 8 – 9 – 10 – JACK – QUEEN – KING。
例如:我通过 JSON 文件恢复了用户的 10 张随机卡片:
{
"cards":[
{
"category":"DIAMOND",
"value":"TEN"
},
{
"category":"CLUB",
"value":"ACE"
},
{
"category":"DIAMOND",
"value":"QUEEN"
},
{
"category":"SPADES",
"value":"SEVEN"
},
{
"category":"DIAMOND",
"value":"NINE"
},
{
"category":"HEART",
"value":"QUEEN"
},
{
"category":"CLUB",
"value":"TEN"
},
{
"category":"HEART",
"value":"FIVE"
},
{
"category":"HEART",
"value":"SEVEN"
}
]
}
排序的牌变成:ACE CLUB /FIVE HEART / SIX CLUB / SEVEN HEART / SEVEN SPADES / NINE DIAMOND / TEN DAIMOND /TEN CLUB / QUEEN DIAMOND / QUEEN HEART 我不知道如何开始?
在这种情况下,您可以使用 usort()
然后应用您的逻辑。在这种情况下,我有两个类别和值数组,以及它们的顺序。在下方,您可以看到 usort
函数,它通过两个数组的索引比较值。你可以把函数变小,但我这样写是为了更好地理解:
<?php
$category_order = ["CLUB", "SPADES", "HEART", "DIAMOND"];
$value_order = ["ACE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING"];
$input = '{
"cards":[
{
"category":"DIAMOND",
"value":"TEN"
},
{
"category":"CLUB",
"value":"ACE"
},
{
"category":"DIAMOND",
"value":"QUEEN"
},
{
"category":"SPADES",
"value":"SEVEN"
},
{
"category":"DIAMOND",
"value":"NINE"
},
{
"category":"HEART",
"value":"QUEEN"
},
{
"category":"CLUB",
"value":"TEN"
},
{
"category":"HEART",
"value":"FIVE"
},
{
"category":"HEART",
"value":"SEVEN"
}
]
}';
$input = json_decode($input, true);
$cards = $input["cards"];
usort($cards, function($a, $b) use ($category_order, $value_order) {
if (array_search($a['category'], $category_order) > array_search($b['category'], $category_order)) {
return 1;
} elseif (array_search($a['category'], $category_order) == array_search($b['category'], $category_order)) {
if(array_search($a['value'], $value_order) > array_search($b['value'], $value_order)) {
return 1;
} else {
return -1;
}
} else {
return -1;
}
});
print_r($cards);
输出:
Array
(
[0] => Array
(
[category] => CLUB
[value] => ACE
)
[1] => Array
(
[category] => CLUB
[value] => TEN
)
[2] => Array
(
[category] => SPADES
[value] => SEVEN
)
[3] => Array
(
[category] => HEART
[value] => FIVE
)
[4] => Array
(
[category] => HEART
[value] => SEVEN
)
[5] => Array
(
[category] => HEART
[value] => QUEEN
)
[6] => Array
(
[category] => DIAMOND
[value] => NINE
)
[7] => Array
(
[category] => DIAMOND
[value] => TEN
)
[8] => Array
(
[category] => DIAMOND
[value] => QUEEN
)
)