Reputation: 11
I wanna create an array or a function n php with something like this:
for index between 1-20 returns "type 1"
for index between 20-25 returns "type 2"
for index between 25-35 returns "type 1"
for index between 35-40 returns "type 3"
for index between 40-60 retusn "type 2".
How can I do this?
For example id 1, id 22, id 50 can be type 1, but also id 2, id 23, id 51 are type 2.
I've tried this:
function getSkinType($id) {
$skins = array(
0,13,40,46,47,59,60,72,73,76,82,83,84,85,90,91,93,98,102,103,104,105,106,107,108,109,110,111,112, 113,114,115,116,120,121,123,124,125,126,127,128,141,147,150,163,164,165,166,169,170,173,174,175,177,181,185,186,187,189,191,193,194,192,195,203,204,216,219,221,223,228,233,240,258,259,263,269,270,271,272,290,292,293,294,295,296,297,298,299,3 => "premium",
2,7,9,11,14,15,16,17,19,20,21,22,18,23,24,25,28,29,30,32,33,34,35,36,41,43,44,45,48,51,52,57,58,63,64,66,67,69,88,92,95,96,97,100,101,122,131,133,138,139,140,142,143,148,154,155,156,167,171,172, 176,179,180,182,183, 184,188,190,201,202,206,214,215,222,224,225,226,227,234,238,247,248,249,250,251,252 => "normal",
1,12,27,31,37,50,56,78,79,129,134,135,136,137,152,153,157,158,159,160,161,162,196,200,212 => "default"
);
return $skins[$id];
}
Upvotes: 0
Views: 28
Reputation: 550
Try this
function getSkinType($id) {
$grouping = [
'type 1' => [1, 13, 20, 25, 35], // [ ids ]
'type 2' => [20, 25],
'type 3' => [35, 40],
];
foreach($grouping as $type => $range) {
if (in_array($id, $range)) {
return $type;
}
}
return 'Type not found'; // default text here
}
echo getSkinType(13); // "type 1"
Upvotes: 0
Reputation: 42304
One possible approach would be to simply make use of an index in the $kins
array to represent the category. This could be passed into the function itself. For example, if you wanted to retrieve the 82
skin (at index 10
of the first category), you could use the following:
<?php
function getSkinType($category, $id) {
$skins = array(
[0,13,40,46,47,59,60,72,73,76,82,83,84,85,90,91,93,98,102,103,104,105,106,107,108,109,110,111,112, 113,114,115,116,120,121,123,124,125,126,127,128,141,147,150,163,164,165,166,169,170,173,174,175,177,181,185,186,187,189,191,193,194,192,195,203,204,216,219,221,223,228,233,240,258,259,263,269,270,271,272,290,292,293,294,295,296,297,298,299,3],
[2,7,9,11,14,15,16,17,19,20,21,22,18,23,24,25,28,29,30,32,33,34,35,36,41,43,44,45,48,51,52,57,58,63,64,66,67,69,88,92,95,96,97,100,101,122,131,133,138,139,140,142,143,148,154,155,156,167,171,172,176,179,180,182,183, 184,188,190,201,202,206,214,215,222,224,225,226,227,234,238,247,248,249,250,251,252],
[1,12,27,31,37,50,56,78,79,129,134,135,136,137,152,153,157,158,159,160,161,162,196,200,212]
);
return $skins[$category][$id];
}
echo getSkinType(0, 10); // 82
In the above, category 0
corresponds to premium, 1
corresponds to normal
, and 2
corresponds to default
. If you wanted, you could set up variables for these and use the variables instead.
An alternative approach would be to use associative arrays rather than a category index, but that accomplishes essentially the same thing.
Upvotes: 1