Jump to content

Order Array


karl-93

Recommended Posts

I ordered an array:

 

code:

for ($ x = 0; $ x <count ($ arr); $ x + +) {

for ($ y = 0; $ y <count ($ arr); $ y + +) {

if ($ arr [$ x] <$ arr [$ y]) {

$ temp = $ arr [$ x];

$ arr [$ x] = $ arr [$ y];

$ arr [$ y] = $ temp;

}

}

}

 

 

The code works ... now I have to print the array, formatted as follows:

 

[5] => -9

 

I have to keep the original key and returning the key and value according to the following format described above.

 

How should I do?

 

 

Thank you!

 

 

Ps.

I can not use sorting functions.

Link to comment
https://forums.phpfreaks.com/topic/273135-order-array/
Share on other sites

:code_tags:

 

If the code above is your actual code you could of used the sort() function; I appreciate you said you can't but never the less.

 

To print the code use:

 

foreach($arr as $k => $v) 
  echo $k . " => " . $v . "<br />";

 

This sentence

 

I have to keep the original key and returning the key and value according to the following format described above.

 

makes absolutely no sense to me.

Link to comment
https://forums.phpfreaks.com/topic/273135-order-array/#findComment-1405545
Share on other sites

I can not use sorting functions because the tutorial says not to.

 

Here is the array:

 

 

$arr=array(55,32,3,89,88,-13,155,485,-77,40,91,13,77,44,-511,2);

 

This sentence:

I have to keep the original key and returning the key and value according to the following format described above.

 

It means that I have to print this way:

 

 

14 => -511

8 => -77

5 => -13

15 => 2

...

7 => 485

 

I do not print well:

 

0 => -511

1 => -77

2 => -13

3 => 2

...

15 => 485

 

 

How should I do?

Link to comment
https://forums.phpfreaks.com/topic/273135-order-array/#findComment-1405549
Share on other sites

You would need to store the key value pair somewhere else. I would then use the usort() function to make a comparison and have PHP sort it.

 

For example, if you rework the array to form another array in the format array(0 => "key:value", 1 => "key:value") you can use:


usort($array, function($a, $B) {
   $a = explode(":", $a);
   $b = explode(":", $B);
   if($a[1] < $b[1]) {
       return -1;
   } elseif($a[1] == $b[1]) {
       return 0;
   } else {
       return 1;
   }
});

Link to comment
https://forums.phpfreaks.com/topic/273135-order-array/#findComment-1405608
Share on other sites

Sort an array of keys based on their values, then after that array of keys is sorted, grab the original values for each key.  

 

Untested, but rough idea:

$keys = array_keys($arr);
for ($i=0; $i<count($keys); $i++){
  for ($n=0; $n<$count($keys); $n++){
     $a = $arr[$keys[$i]];
     $b = $arr[$keys[$n]];
     if ($a < $B){
        $tmp=$keys[$n];
        $keys[$n] = $keys[$i];
        $keys[$i] = $tmp;
     }
  }
}

$final = array();
foreach ($keys as $key){
  $final[$key]=$arr[$key];
}

print_r($final);

 

Link to comment
https://forums.phpfreaks.com/topic/273135-order-array/#findComment-1405619
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.