Jump to content

Recommended Posts

Hi. I'm trying to find a sort function that will sort by the value without changing the keys. Is their one, or do I have to make this? I read that array_multisort doesn't change the key if it's a string, but I use a 9 digit numerical string which just gets converted to a number.

Link to comment
https://forums.phpfreaks.com/topic/89842-solved-sort-without-changing-keys/
Share on other sites

instead of using the value why not use the key..

 

<?php
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
ksort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}
?>

 

this works the same !

instead of using the value why not use the key..

 

<?php
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
ksort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}
?>

 

this works the same !

 

 

Yes.. I'm familiar with ksort. I'd rather find something that sorts values without changing the keys as I stated in my question. Since the values are not unique, I can't just switch the key and value. And I'd prefer not to change the id from '901234567' to something like 'rp1234567'

I'm making a roommate matching website. Each person as an 'id' like: '901234567'  ((all numerical string))

 

I have an array of people with their possible matches and scores.

 

 

$roommates[$id] = $score; //for each person

$person[$id] = $roommates;

 

so this is how it would look for person['123456789']

 

foreach ($person['123456789'] as $key => $value)
{
  echo "$key: $value  <br>";
}

 

This would display

 

987654321: 77

234567890: 66

123456780: 86

...

...

 

the score's i store are 51-100 so their will be plenty of duplicates as I can't use the score as a key.

 

When I sort, I lose the $key which I need to know the id of the person, so what I need is a sort function that will not re-index from 0-n, but rather keep my key as the id.

Are you asking if I'm sorting by the value ($score) which I've said several times? Or are you asking if I want to sort in ascending or descending order? Not sure how that matters...

 

One last time....

 

Sorting by the value (which has duplicates)

Don't want to change the key (which obviously doesn't have duplicates)

Can't use array_flip function since the value i'm sorting by has duplicates.

Don't feel like creating my own sort that does this.

I just was wondering if their was a function like no_change_keys_sort($array, ASC | DESC | ...)

 

$fruits = array(5=>"lemon", 4=>"orange", 2=>"banana", 6=>"apple");
$fruits2 = array(5=>"lemon", 4=>"orange", 2=>"banana", 6=>"apple");
sort($fruits);
$x= 0;
$z= array();
foreach ($fruits as $val){
$new = array($val);
$result = array_intersect($fruits2, $new);
print_r($result);echo '<br>';
}

 

this?

output

Array ( [6] => apple )

Array ( [2] => banana )

Array ( [5] => lemon )

Array ( [4] => orange )

Well that works if their are no duplicate results. For that, just doing array_flip before and after a ksort would be easier. If their are duplicates, I get something like:

 

Array ( [4] => apple [6] => apple )

Array ( [4] => apple [6] => apple )

Array ( [5] => lemon )

Array ( [2] => pear )

 

I want:

 

4 => apple

6 => apple

5 => lemon

2 => pear

 

I really don't want to do nested arrays.

Where exactly are you getting this data?  And what formula are you using to calculate the score?  If you're using MySQL you might be able to just shove everything over to the DB engine and have it sort the results for you.

Where exactly are you getting this data?  And what formula are you using to calculate the score?  If you're using MySQL you might be able to just shove everything over to the DB engine and have it sort the results for you.

 

if your not using SQL  you may use SJF algorithm or shortest job first.. i believe you can compare string in php like a<b is true.. 

Where exactly are you getting this data?  And what formula are you using to calculate the score?  If you're using MySQL you might be able to just shove everything over to the DB engine and have it sort the results for you.

 

I'm calculating all the data. I take a quesionaire from 2 people and generate a score based on similar matches. None of the data below is in the database. I just store the person id and their answers in the database. One person will have an array like this...

 

$person = Array($id => $roommates) where $id is the person's id and $roommates is an array of possible roommates like so:

 

$roommate = Array($id => $score) where $id is the roommate's id and $score is the compatibility of the 2 people.

 

 

So for person [100] we might have

 

101: 66

102: 77

103: 88

104: 77

105: 99

106: 66

 

I want to sort in descending order and get:

 

105: 99

103: 88

102: 77

104: 77

101: 66

106: 66

 

(where for the elements 102/104 and 101/106 the order doesn't really matter.. 104 could come before 102, but the way I have it above is prefered)

i don't know if this is the easiest, but here is one way:

 

code:

<?php
  $list = array(
    '987654321' => 77,
    '234567890' => 66,
    '123456780' => 86,
  );
  $id_list = array_keys($list);
  $score_list = array_values($list);
  array_multisort($score_list,$id_list);
  $new_list = array_combine($id_list,$score_list);
  print_r($new_list);
?>

 

output:

Array
(
    [234567890] => 66
    [987654321] => 77
    [123456780] => 86
)

i don't know if this is the easiest, but here is one way:

 

code:

<?php
  $list = array(
    '987654321' => 77,
    '234567890' => 66,
    '123456780' => 86,
  );
  $id_list = array_keys($list);
  $score_list = array_values($list);
  array_multisort($score_list,$id_list);
  $new_list = array_combine($id_list,$score_list);
  print_r($new_list);
?>

 

output:

Array
(
    [234567890] => 66
    [987654321] => 77
    [123456780] => 86
)

 

Thank you. Thank you. Thank you!! Works great.

Just found these 2 functions:

 

asort: sorts ascending without changing keys

arsort: sorts descending without changing keys

 

 

although arsort sorts the indexes or duplicate values in descending order rather than keeping them in their original order in the table.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.