0

Good day.

I would be like get 3 keys from $arr where value $arN[0] will be more than other...

Code:

$ar1=array(201,281);
$ar2=array(1252760,1359724);
$ar3=array(452760,34349724);
$ar4=array(1260,134344);
$ar5=array(232750,1359724);
$ar6=array(60,1439724);

$arr[]=array(6299927 => $ar1);
$arr[]=array(1252760 => $ar2);
$arr[]=array(3432444 => $ar3);
$arr[]=array(3435543 => $ar4);
$arr[]=array(7645466 => $ar5);
$arr[]=array(4574534 => $ar6);

Next function sorting array $a descending:

function cmp($a, $b)
{
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? 1 : -1;
}

$a = array(3, 2, 5, 6, 1);

usort($, "cmp");

foreach ($a as $key => $value) {
    echo "$key: $value\n";
}
outpoot: 0:6 1:5 2:4 3:2 4:1

But how change this function for my example(for my big array)?

Tell me please how make this?

How write right?

  • Do you want to get the index, if 201 is greater than 281 or 201 is greater than 1252760? – Starx Jun 29 '13 at 02:00
  • What do you mean by "unsort"? Is that a typo for "usort"? – Barmar Jun 29 '13 at 02:03
  • possible duplicate of [How do I sort a multidimensional array in php](http://stackoverflow.com/questions/96759/how-do-i-sort-a-multidimensional-array-in-php) – Barmar Jun 29 '13 at 02:05
  • @Starx 201 is greater than 1252760 –  Jun 29 '13 at 02:05
  • @Barmar yes whis usort –  Jun 29 '13 at 02:05
  • This is a very strange array. Each element of the array is an associative array with just one element with a different key. Why don't you use those as the keys of `$arr`? – Barmar Jun 29 '13 at 02:18

3 Answers3

0

Very confusing but I think you are looking for something like this. It will give you the index of the array whose first item is greater than the first item in the next array.

foreach($arr as $key => $array) {
    if($array[0] > $arr[$key+1][0]) {
       echo $key;
    }
}
Starx
  • 72,283
  • 42
  • 174
  • 253
  • @Alex, Of course my code is working. I think you meant, you are not getting the result you expected. – Starx Jun 29 '13 at 02:39
0
$dates = array();
foreach ($arr as $key => $row) {
    $subkeys = array_keys($row); // Elements are one-element associative arrays
    $dates[$key]  = $row[$subkeys[0]][0]; // Get the element, then get its [0] sub-element

}
array_multisort($dates, SORT_DESC, $arr);
print_r($arr);
Barmar
  • 596,455
  • 48
  • 393
  • 495
0
$ans_array= array();

            // for shuffle array without repeat
            for($q=0;$q<count($arr);$q++)
            {
                $n = rand(0,count($arr));
                if($ans_array[$n] == NULL || $ans_array[$n] == '')
                {   
                     $ans_array[$n] = $arr[$q];
                }
                else
                {     if($q==0) $q=0;
                      else $q = $q-1;
                }
            }