How would I sort this by name or by id? Array ( [0] => Array ( [id] => 1 [name] => green ) [1] => Array ( [id] => 4 [name] => blue ) [2] => Array ( [id] => 5 [name] => black )
you could write your own bubble sorting function. all a bubble sort does is itterate through the array several times, and swap values according to a condition. check out this page : http://psoug.org/snippet/PHP-Bubblesort-routine_362.htm pm me if you need any help with it.
Here is a quick script I wrote for you: $arr1 = $arr2 = array( array('id' => 1, 'name' => 'green'), array('id' => 4, 'name' => 'blue'), array('id' => 5, 'name' => 'black') ); function by_id($a, $b) { $c = $a['id']; $d = $b['id']; return $c < $d ? -1 : ($c == $d ? 0 : 1); } function by_name($a, $b) { return strcmp($a['name'], $b['name']); } usort($arr1, "by_id"); usort($arr2, "by_name"); PHP: Might not work exactly as expected, wrote it directly without testing it, but you should get the idea ** That won't work considering he wants to sort an array or arrays
asort() sorts associative arrays brother. what he posted, is 3 associative arrays inside an array (Multidimensional array). array_merge() will take each associative array and combine into one. Then run asort(). Done and faster performance.
Guess you don't understand the question in the OP. Sorting arrays is different from sorting an array or arrays. asort only sorts the first dimension, array of arrays = more than 1 dimesion. I don't know how else I could put that lol ** He posted an array of associative arrays making it 2 dimensions, not just an associative array which would be 1 dimension
Another one from php.net: <?php $ar = array( array("10", 11, 100, 100, "a"), array( 1, 2, "2", 3, 1) ); array_multisort($ar[0], SORT_ASC, SORT_STRING, $ar[1], SORT_NUMERIC, SORT_DESC); var_dump($ar); ?> PHP:
Lol, I seriously think you need to work on your problem solving skills... Why in the world would he want to merge the arrays if he wants to sort the arrays inside the array? Please, I want to see your solution, go ahead and sort this array: $arr = array( array('id' => 1, 'name' => 'green'), array('id' => 4, 'name' => 'blue'), array('id' => 5, 'name' => 'black') ); // This is the same array in the OP translated into PHP PHP: Go ahead and apply your solution and sort that by name.
Wow, taking things a bit personal there bub? Ok here you go, with my original answer.... asort() <?php if (!empty($_REQUEST['sorder'])) { if ($_REQUEST['sorder'] == 'id') { $given = 'id'; } elseif ($_REQUEST['sorder'] == 'name') { $given = 'name'; } else { die('Invalid sort option. You must send a sorder value of "id" or "name".'); } } else { die('Invalid sort option. You must send a sorder value of "id" or "name".'); } $arr = array(array('id' => 1, 'name' => 'green'),array('id' => 5, 'name' => 'black'),array('id' => 4, 'name' => 'blue')); function my_msort($arr,$sorder) { foreach($arr as $key=>$val) $pre[$key] = strtolower($val[$sorder]); asort($pre); foreach($pre as $k=>$v) $res[] = $arr[$k]; return $res; } $sp = my_msort($arr,$given); echo '<pre>'; var_dump($sp); echo '</pre>'; ?> PHP: See it in action: Sort by id: http://crackfeed.com/_test.php?sorder=id Sort by name: http://crackfeed.com/_test.php?sorder=name I guess I am just some dumb hick though, right? Any other challenges? I love challenges, especially with my Canadian neighbors! I win, now what's THAT all aboot? I've been coding PHP since it was released. I wrote my first program in BASIC when I was 7 years old. Did I mention that I am a member of Mensa too? Oh, one more thing Mr. X319; to answer your completely stupid question: It has to come out of the array at some point or it serves no purpose. Prior to that point, you can sort.
Mr. X319, If you have any more silly questions or attitude, I will answer or choose to ignore it after my morning coffee tomorrow, it is late.
Hey, nothing personal Lol. Just that your replies sound pretty stupid, and you keep editing and changing what you say. First you make it sound like using asort directly will solve the problem. Then you say that what he posted is an associative array and you also include an example or a regular associative array which was 1 dimension, I replied telling you its an array of associative arrays - you edited your post to correct that which I saw after I posted my message, and you pointed this out too so it was OK. Next you edit your post and just restate what I said in my post appearing right after about the multidimensional array, you also mention array_merge, which isn't helpful + you never used array_merge in your code like you said. And then you post an array or mixed arrays which again doesn't compare with the OP's array, but I guess you were just trying to give extra info so we'll let that slide too. However, I guess all this is OK since your a member of Mensa, right? And after all that, you finally post something that has a bit of value, so good job on that I never posted my solution to compete with you, it was a rough draft. But since your really asking for something, here you go: <?php $arr = array( array('id' => 1, 'name' => 'green'), array('id' => 5, 'name' => 'black'), array('id' => 4, 'name' => 'blue') ); function sort2d($arr, $key) { usort($arr, create_function('$a, $b', "return strcmp(\$a['{$key}'], \$b['{$key}']);")); return $arr; } print_r(sort2d($arr, 'id')); print_r(sort2d($arr, 'name')); ?> PHP: My function is half of yours. You can see it in action: http://nsyed.com/t/2192707.php Anyway, back to work.
I was trying to point the poster to docs to learn how to do it, I am not huge on giving people answers. That is why I will almost always give starting hints. A good programmer has to find a starting point and work through and around it. My style of helping may be different, but I am not helping to do free work, I am helping to teach. I, at that point, had also been ending a 18 hour shift and was exhausted. I was posting different ways to work with it. You complained about how I was pointing out array merge so I did not go that route in order to avoid an annoying and time wasting flaming thread. Wow, you do not get out much I am guessing. You think that because you think a person does something for the motivation you think, that your assumptions are correct. People think in different ways, have different methodologies and one cannot make assumptions that because something does not make sense to them, that the poster is stupid. Yes, my 155 IQ does tend to help me out a lot, to answer your question. I have Asperger's syndrome and often times I do not express myself well. However, this does not make me stupid, I just happen to be more analytical than social. Yes I go back and edit posts, i see that what I wrote might not make sense and I try to revise to fix that. No, just not trying to code for every person that needs help, one cannot learn without doing. my answer is given and it works flawlessly, so I am unsubscribing from this thread, I have work to do now.