Jump to content

Check order of Array from 1 through 9 ONLY


Monkuar
Go to solution Solved by Barand,

Recommended Posts

For example for my Skill Tree:

array (size=9)
0 =>
array (size=2)
'Skill_Id' => string '1' (length=1)
'Skill_Points' => string '0' (length=1)
1 =>
array (size=2)
'Skill_Id' => string '2' (length=1)
'Skill_Points' => string '0' (length=1)
2 =>
array (size=2)
'Skill_Id' => string '3' (length=1)
'Skill_Points' => string '0' (length=1)
3 =>
array (size=2)
'Skill_Id' => string '4' (length=1)
'Skill_Points' => string '1' (length=1)
4 =>
array (size=2)
'Skill_Id' => string '5' (length=1)
'Skill_Points' => string '1' (length=1)
5 =>
array (size=2)
'Skill_Id' => string '6' (length=1)
'Skill_Points' => string '1' (length=1)
6 =>
array (size=2)
'Skill_Id' => string '7' (length=1)
'Skill_Points' => string '0' (length=1)
7 =>
array (size=2)
'Skill_Id' => string '8' (length=1)
'Skill_Points' => string '1' (length=1)
8 =>
array (size=2)
'Skill_Id' => string '9' (length=1)

For the Skill_Id, it must be in order when checking server side.  1 through 9. (or 9 can be changed by me whenever).

 

So, I have:

 

$skillrange = range(1,$amountofskills);

 

Which will output:

array (size=9)
0 => int 1
1 => int 2
2 => int 3
3 => int 4
4 => int 5
5 => int 6
6 => int 7
7 => int 8
8 => int 9

But, how do I check this order from my original array? I need to check My Skill_ID values in order from my original array and make sure they are 1 through 9. (So a user cannot tamper the data)

 

Edit: I've come up with this solution since making this thread:

 

 

$array1 = $skillidarray;
$array2 = $skillrange;
if (!empty(array_diff_assoc($array1, $array2))){
echo "Your skill tree data is out of order, please report this to an administrator.";
}

 

Something like this would work right?

Edited by Monkuar
Link to comment
Share on other sites

Your array is a two dimensional array. het main array or maybe you like to call it the outer array has 9 elements. All the 9 elements has a value 'array'

 

check it out:

foreach($yourarray as $arr)
   echo $arr;

besides warnings you will see a value that is all the time the same. That is why a regular sort function wont work

 

Luckly php have usort(). with usort you can give a callback function to compare two elements in the array on just the way you like.

<?php
function sortSkillId($a, $b)
{
    return $a['Skill_Id'] > $b['Skill_Id'];
}

function sortSkillPoints($a, $b)
{
    return $a['Skill_Points'] > $b['Skill_Points'];
}

echo '<pre>';
    
// sort the array on the Skill_Id elements
usort($yourarray, 'sortSkillId');
print_r($yourarray);

// sort the array on the Skill_Id elements
usort($yourarray, 'Skill_Points');
print_r($yourarray);

echo '</pre>';

?>
Link to comment
Share on other sites

Also you can make the sort routine more advanced. What if the skill points are equal?

 

Maybe you would like to have the lowest id on top in that case.

<?php
function advancedSort($a, $b)
{
    if($a['Skill_Points'] == $b['Skill_Points'])
		return $a['Skill_Id'] > $b['Skill_Id'];

	return $a['Skill_Points'] > $b['Skill_Points'];
}

echo '<pre>';
    
// sort the array first on points and if equel on id
usort($yourarray, 'advancedSort');
print_r($yourarray);

echo '</pre>';

?>
Link to comment
Share on other sites

Thanks Barand, that worked.

 

Frank_b, not sure if I need that type of sortment at the moment. The current way checks only if one is out of line (from the range(1,9) then it error's out. But in the future, if I need to sort the Id's for a specific case, I'll use your example.

 

Thank guys, been forever since I've posted here. 

Edited by Monkuar
  • Like 1
Link to comment
Share on other sites

@Frank_b

 

Custom sort functions are required to return -ve, 0, +ve depending an whether $a is lower, equal or higher than $b. You are returning boolean values.

 

So to sort on (Points DESC, Id ASC) it would be

function advancedSort($a, $b)
{
    $tmp = $b['Skill_Points'] - $a['Skill_Points'];
    if ($tmp==0) { // points are the same
        return $a['Skill_Id'] - $b['Skill_Id']; 
    }
    else return $tmp;
}


$arr = array(
    array ('Skill_Id' => 2, 'Skill_Points' => 5),
    array ('Skill_Id' => 3, 'Skill_Points' => 2),
    array ('Skill_Id' => 5, 'Skill_Points' => 5),
    array ('Skill_Id' => 4, 'Skill_Points' => 1),
    array ('Skill_Id' => 1, 'Skill_Points' => 3),
    );

usort($arr, 'advancedSort');

echo '<pre>',print_r($arr, true),'</pre>';

Resulting in

Array
(
    [0] => Array
        (
            [Skill_Id] => 2
            [Skill_Points] => 5
        )

    [1] => Array
        (
            [Skill_Id] => 5
            [Skill_Points] => 5
        )

    [2] => Array
        (
            [Skill_Id] => 1
            [Skill_Points] => 3
        )

    [3] => Array
        (
            [Skill_Id] => 3
            [Skill_Points] => 2
        )

    [4] => Array
        (
            [Skill_Id] => 4
            [Skill_Points] => 1
        )

)

However, when you sort() a two dimensional array it will sort on the values of the first elements. So if you just want a sort on Skill_id

sort($arr);

giving

Array
(
    [0] => Array
        (
            [Skill_Id] => 1
            [Skill_Points] => 3
        )

    [1] => Array
        (
            [Skill_Id] => 2
            [Skill_Points] => 5
        )

    [2] => Array
        (
            [Skill_Id] => 3
            [Skill_Points] => 2
        )

    [3] => Array
        (
            [Skill_Id] => 4
            [Skill_Points] => 1
        )

    [4] => Array
        (
            [Skill_Id] => 5
            [Skill_Points] => 5
        )

)
  • Like 1
Link to comment
Share on other sites

Hi Barand,

 

I just changed the > to < to change the order direction Skill_Points from ascending to descending like you did and i have exactly the same results. 

 

tried it with more elements and still the results are equal. I looked on php.net and saw that they descriped it like you are telling but i think because of php's automatic type casting it does not make any sence

if you give a boolean or a int back. a boolean will be converted to an integer when needed. 

 

There is also no need to return a negative, positive or zero back because zero will stay unhandled.

Note:
If two members compare as equal, their relative order in the sorted array is undefined.

Long story short:

two manners and same result.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.