Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 286
  • Last Modified:

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);
0
kadin
Asked:
kadin
  • 4
  • 2
1 Solution
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
kadinAuthor Commented:
That was it. Thanks.
0
 
Ray PaseurCommented:
Great question!  Thanks for the points and thanks for using E-E, ~Ray
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now