Advertisement:

Author Topic: Sorting an Array  (Read 575 times)

Offline Biology Forums

  • SMF Hero
  • ******
  • Posts: 3,747
    • StudyForcePS on Facebook
    • @studyforceps on Twitter
Sorting an Array
« on: August 31, 2019, 05:59:09 PM »
This is a basic PHP question.

I have an array called $array like this:

Code: [Select]
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?
« Last Edit: August 31, 2019, 06:10:11 PM by Study Force »

Offline SychO

  • Lead Customizer
  • SMF Hero
  • *
  • Posts: 1,526
  • Gender: Male
    • SychO9 on GitHub
    • SychO
Re: Sorting an Array
« Reply #1 on: August 31, 2019, 06:11:16 PM »
Use usort

Code: [Select]
<?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;
});


Imazighen

Online shawnb61

  • Developer
  • SMF Hero
  • *
  • Posts: 1,433
    • sbulen on GitHub
Re: Sorting an Array
« Reply #2 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'];
Address the process rather than the outcome.  Then, the outcome becomes more likely.   - Fripp

Offline SychO

  • Lead Customizer
  • SMF Hero
  • *
  • Posts: 1,526
  • Gender: Male
    • SychO9 on GitHub
    • SychO
Re: Sorting an Array
« Reply #3 on: August 31, 2019, 06:21:52 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

Imazighen

Online Pipke

  • Full Member
  • ***
  • Posts: 478
    • [NRNS]
Re: Sorting an Array
« Reply #4 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

Code: [Select]
$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;

Offline Biology Forums

  • SMF Hero
  • ******
  • Posts: 3,747
    • StudyForcePS on Facebook
    • @studyforceps on Twitter
Re: Sorting an Array
« Reply #5 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!

Offline Antechinus

  • SMF Friend
  • SMF Super Hero
  • *
  • Posts: 24,756
  • Master of BBC Abuse
Re: Sorting an Array
« Reply #6 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.

Offline Biology Forums

  • SMF Hero
  • ******
  • Posts: 3,747
    • StudyForcePS on Facebook
    • @studyforceps on Twitter
Re: Sorting an Array
« Reply #7 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...?

Offline Antechinus

  • SMF Friend
  • SMF Super Hero
  • *
  • Posts: 24,756
  • Master of BBC Abuse
Re: Sorting an Array
« Reply #8 on: September 01, 2019, 08:02:47 PM »
I like the thinner bars. I'd go with that for now.

Offline Biology Forums

  • SMF Hero
  • ******
  • Posts: 3,747
    • StudyForcePS on Facebook
    • @studyforceps on Twitter
Re: Sorting an Array
« Reply #9 on: September 01, 2019, 08:11:29 PM »
Thanks, I also placed the question in the header. Now there's less ambiguity
« Last Edit: September 01, 2019, 09:01:26 PM by Study Force »

Offline Antechinus

  • SMF Friend
  • SMF Super Hero
  • *
  • Posts: 24,756
  • Master of BBC Abuse
Re: Sorting an Array
« Reply #10 on: September 01, 2019, 08:14:14 PM »
Yup, that works too.