Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: Biology Forums on August 31, 2019, 05:59:09 PM

Title: Sorting an Array
Post by: Biology Forums on August 31, 2019, 05:59:09 PM
This is a basic PHP question.

I have an array called $array like this:

Array
(
    [0] => Array
        (
            [ID_CHOICE] => 0
            [label] => Amazon or alike
            [votes] => 48
            [votedThis] => -1
        )

    [1] => Array
        (
            [ID_CHOICE] => 1
            [label] => Specialty textbook website
            [votes] => 4
            [votedThis] => -1
        )

    [2] => Array
        (
            [ID_CHOICE] => 2
            [label] => Campus bookstore
            [votes] => 22
            [votedThis] => 2
        )

    [3] => Array
        (
            [ID_CHOICE] => 3
            [label] => Textbook swap service
            [votes] => 1
            [votedThis] => -1
        )

    [4] => Array
        (
            [ID_CHOICE] => 4
            [label] => E-Book from Publisher
            [votes] => 5
            [votedThis] => -1
        )

    [5] => Array
        (
            [ID_CHOICE] => 5
            [label] => Friends
            [votes] => 5
            [votedThis] => -1
        )

    [6] => Array
        (
            [ID_CHOICE] => 6
            [label] => Other
            [votes] => 14
            [votedThis] => -1
        )

)


I want to sort [ 0 ] through [ 6 ] in ascending order based on [votes] (from highest to lowest). In this case [ 0 ] happens to be the highest already, but that's not always the case obviously. I tried arsort($array), but I can't see to understand how its done.

Any help?
Title: Re: Sorting an Array
Post by: SychO on August 31, 2019, 06:11:16 PM
Use usort (https://www.php.net/manual/en/function.usort.php)


<?php

// If you're using php7 you can just use the spaceship operator
usort($array, function($item1$item2) {
    return 
$item2['votes'] <=> $item1['votes'];
});

// otherwise
usort($array, function($item1$item2) {
    if (
$item2['votes'] == $item1['votes'])
        return 
0;

    return (
$item2['votes'] < $item1['votes']) ? -1;
});

Title: Re: Sorting an Array
Post by: shawnb61 on August 31, 2019, 06:18:31 PM
Yep, usort.

Since the function needs to return an integer, and the size of the integer is irrelevant (only the sign is), you can go even simpler &:
return $item1['votes'] - $item2['votes'];
Title: Re: Sorting an Array
Post by: SychO on August 31, 2019, 06:21:52 PM
Quote from: shawnb61 on August 31, 2019, 06:18:31 PM
Yep, usort.

Since the function needs to return an integer, and the size of the integer is irrelevant (only the sign is), you can go even simpler &:
return $item1['votes'] - $item2['votes'];

ah nice, simpler indeed
Title: Re: Sorting an Array
Post by: Pipke on August 31, 2019, 06:53:45 PM
maybe this helps you. result will be text: Highest value of votes is 48 and is found in array -> 0


$array = array(
    0 => array
        (
            'ID_CHOICE' => 0,
            'label' => 'Amazon or alike',
            'votes' => 48,
            'votedThis' => -1,
        ),

    1 => array
        (
            'ID_CHOICE' => 1,
            'label' => 'Specialty textbook website',
            'votes' => 4,
            'votedThis' => -1,
        ),

2 => array
        (
            'ID_CHOICE' => 2,
            'label' => 'Campus bookstore',
            'votes' => 22,
            'votedThis' => 2,
        ),

    3 => array
        (
            'ID_CHOICE' => 3,
            'label' => 'Textbook swap service',
            'votes' => 1,
            'votedThis' => -1,
        ),

    4 => array
        (
            'ID_CHOICE' => 4,
            'label' => 'E-Book from Publisher',
            'votes' => 5,
            'votedThis' => -1,
        ),

    5 => array
        (
            'ID_CHOICE' => 5,
            'label' => 'Friends',
            'votes' => 5,
            'votedThis' => -1,
        ),

    6 => array
        (
            'ID_CHOICE' => 6,
            'label' => 'Other',
            'votes' => 4,
            'votedThis' => -1,
        ),
);

$highest_value = 0;

foreach ($array as $key => $val) {
    if ($val['votes'] > $highest_value)
{
$highest_value = $val['votes'];
$result = $key;
}
}

echo 'Highest value of votes is '.$highest_value.' and is found in array -> '.$result;
Title: Re: Sorting an Array
Post by: Biology Forums on September 01, 2019, 11:09:30 AM
Thanks everyone for contributing. Here's what a bit of code and math algorithms can accomplish.

OLD - See attachment (uses images and bar widths never span the container)

NEW - https://biology-forums.com/index.php?topic=547130

Any design suggestions are welcome too!
Title: Re: Sorting an Array
Post by: Antechinus on September 01, 2019, 07:42:10 PM
Looks pretty good, but I think a bit more emphasis on the category headings might be useful. At the moment they tend to get a bit lost against the coloured bars.
Title: Re: Sorting an Array
Post by: Biology Forums on September 01, 2019, 07:50:50 PM
I'm not sure how to do that without compromising the question block. If I increase the font-size from 14px to 18px, the question block becomes less visibly important.

I reduced the block size from 12px height over to 8px. Thought...?
Title: Re: Sorting an Array
Post by: Antechinus on September 01, 2019, 08:02:47 PM
I like the thinner bars. I'd go with that for now.
Title: Re: Sorting an Array
Post by: Biology Forums on September 01, 2019, 08:11:29 PM
Thanks, I also placed the question in the header. Now there's less ambiguity
Title: Re: Sorting an Array
Post by: Antechinus on September 01, 2019, 08:14:14 PM
Yup, that works too.