Solved

# simple array sort question.

Posted on 2014-07-12
279 Views
I am trying to get the index numbers for the highest and second highest values. So 22 and 17 are the values. Index 4 and 5 are what I need. How to do this, thanks.

\$array = array(14,11,5,2,22,17);
//\$max = max(\$array);
//\$max = array_keys(\$array, max(\$array));
//\$max = arsort(\$array);
\$max = sort(\$array);
echo \$max[0] . '<br>';
echo \$max[1] . '<br>';
var_dump(\$max);
0

LVL 107

Expert Comment

Sort functions are documented here:
http://php.net/manual/en/array.sorting.php

I think you want rsort()
0

LVL 107

Expert Comment

Or maybe arsort()?  What do you need the index numbers for?
0

Author Comment

I tried rsort(), but all it prints out is - 1.
0

LVL 107

Accepted Solution

``````<?php // demo/temp_kadin.php
error_reporting(E_ALL);
echo '<pre>';

// SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28474648.html

// TEST DATA FROM THE POST AT EE
\$array = array(14,11,5,2,22,17);

// SORT IN HIGH-TO-LOW, KEEPING KEYS
arsort(\$array);

// GET THE KEYS
\$keys = array_keys(\$array);

// SHOW THE TWO HIGHEST
\$highest = array_slice(\$keys,0,2);
var_dump(\$highest);
``````
0

Author Closing Comment

That was it. Thanks.
0

LVL 107

Expert Comment

Great question!  Thanks for the points and thanks for using E-E, ~Ray
0

## Write Comment

Please enter a first name

Please enter a last name

We will never share this with anyone.

## Featured Post

### Suggested Solutions

Title # Comments Views Activity
get users IP address showing ::1 8 37
php explode 3 28
Impossible arithmetic 8 29
codeIgniter ajax post path question 4 22
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

#### 779 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!