simple array sort question.

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);
kadinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

I think you want rsort()
0
Ray PaseurCommented:
Or maybe arsort()?  What do you need the index numbers for?
0
kadinAuthor Commented:
I tried rsort(), but all it prints out is - 1.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Ray PaseurCommented:
Try this... http://iconoun.com/demo/temp_kadin.php

<?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);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kadinAuthor Commented:
That was it. Thanks.
0
Ray PaseurCommented:
Great question!  Thanks for the points and thanks for using E-E, ~Ray
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.