News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Sorting an Array

Started by Biology Forums, August 31, 2019, 05:59:09 PM

Previous topic - Next topic

Biology Forums

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?

SychO

Use usort


<?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 : 1;
});

Checkout My Themes:
-

Potato  •  Ackerman  •  SunRise  •  NightBreeze

shawnb61

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'];
A question worth asking is born in experience & driven by necessity. - Fripp

SychO

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
Checkout My Themes:
-

Potato  •  Ackerman  •  SunRise  •  NightBreeze

Pipke

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;
"If something is wrong, fix it if you can. But train yourself not to worry: Worry never fixes anything."

Click here to view my mods for SMF

Hey 👋 Did i helped... you like what i do. You can now buy me a coffee! ☕

Biology Forums

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!

Antechinus

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.

Biology Forums

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...?

Antechinus

I like the thinner bars. I'd go with that for now.

Biology Forums

#9
Thanks, I also placed the question in the header. Now there's less ambiguity

Antechinus


Advertisement: