For each individual row of an array, I would like it to sort by a function that I perform on each value.

For example:

Array {

1 => "great",
2 => "bad",
3 => "awesome",

}

and I have a function that would make great a 2, bad a 1, and awesome a 3. So how would I resort the array to make bad and great switch?

If further explanation is required, please tell me what to clarify. (Sorry I'm not good at explaining concepts :p)

Recommended Answers

All 4 Replies

Member Avatar for diafol

Have you checked the array functions in the php manual? That's a good place to start. Your logic doesn't seem to follow any traditional 'sort' option - it appears like a random assignment.

A simple solution would be:

$my_array = array (
    1 => "great",
    2 => "bad",
    3 => "awesome",
);

$my_array[2] = 'great';
$my_array[1] = 'bad';

Is this what you mean?

Well if you really need explanation, no that is not what i mean.

I need it so it sorts by a function that parses the code I get from an array fetched from the MySQL database. What I thought is having a second array and checking the keys and lining them up with the function outputs because simply adding to the array wouldn't work (i think)

Member Avatar for diafol

Am I to understand that you want the array to have the same key as your 'id' value in the db?

How about:

while($data = mysql_fetch_array($myrecordset)){
  $my_array[$data['id']] = $data[$thevalue];
}

Hope I got it right, apologies if not.

uh, i fixed it. I took the array, parsed it through a function, and added the result as an extra key to the array...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.