0

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];
}
3
  • 1
    Your question isn't clear... When you say index 1-20. Do you mean the actual index or just values within that range? Commented May 29, 2019 at 22:32
  • I just wanna make something like a case 1,5,10: return "type 1" but with an array. Values in that range. Commented May 29, 2019 at 22:36
  • indentation and double space Commented May 30, 2019 at 9:04

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I think I didn't explained good. For example I have the ID 13 and I wanna know from what category is. Something like getType($id) and this function to returns type of id.
0

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"

4 Comments

I think I didn't explained good. For example I have the ID 13 and I wanna know from what category is. Something like getType($id) and this function to returns type of id.
Yes this function would return the type. Have you tried it out?
Yes but for example I don't have a specific range, type 1 cand be 1,3,5 and type 2 can be 2,4,6. Something like for id 1 or 3 or 5 return type 1, for id 2 or 4 or 6, like a case statement but i don't want to use case statement.
It has been updated to just check for the value, rather than use range.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.