diamondnular Posted July 14, 2008 Share Posted July 14, 2008 Hi folks, I have two arrays: $e = Array ( [0 ] => Array ( [id] => 3 [value] => a ) [1] => Array ( [id] => 2 [value] => b ) [2] => Array ( [id] => 5 [value] => c ) ) and $d = Array ( [0 ] => 5 [1] => 2 [2] => 3 ) What I want to do is to sort array $e with values of 'id' of its elements to be the same order as array $d, meaning that after sorting, new array will be $f = Array ( [0 ] => Array ( [id] => 5 [value] => c ) [1] => Array ( [id] => 2 [value] => b ) [2] => Array ( [id] => 3 [value] => a ) ) Here is what I did: <?php $a = array('id' => 3,'value' =>'a'); $b = array('id' => 2,'value' =>'b'); $c = array('id' => 5,'value' =>'c'); $d = array('5','2','3'); print_r($d); $e = array(); array_push($e,$a); array_push($e,$b); array_push($e,$c); print_r($e); $f = array(); foreach ($d as $key) { foreach ($e as $x) { if ($x[id] == $key) array_push($f,$x); } } print_r($f); ?> The code does work, and gave me the result I want. But I have two questions here: 1. I got a notice: Use of undefined constant id - assumed 'id'. That should be $x[id] == $key How do I fix this notice? 2. More important: I think there must be an easier way to do the above, using asort or usort or some function that I maybe not know of. The above is doable, but it is too handy, and not very efficient in my opinion. You guys masters in php have any advice? Remember, I am just novice in php Thanks, D. Link to comment https://forums.phpfreaks.com/topic/114621-solved-sort-an-array-by-the-values-of-keys-in-another-array/ Share on other sites More sharing options...
JasonLewis Posted July 14, 2008 Share Posted July 14, 2008 $x['id'] == $key Link to comment https://forums.phpfreaks.com/topic/114621-solved-sort-an-array-by-the-values-of-keys-in-another-array/#findComment-589330 Share on other sites More sharing options...
diamondnular Posted July 14, 2008 Author Share Posted July 14, 2008 $x['id'] == $key Arhhh, how can I make that simple mistake? Anyway, thanks ProjectFear. What about the other question? Can I archive the same thing with sort, asort, usort or something similar? Thanks, D. Link to comment https://forums.phpfreaks.com/topic/114621-solved-sort-an-array-by-the-values-of-keys-in-another-array/#findComment-589641 Share on other sites More sharing options...
JasonLewis Posted July 15, 2008 Share Posted July 15, 2008 I don't know, sorry. Nothing wrong with what your doing though. Link to comment https://forums.phpfreaks.com/topic/114621-solved-sort-an-array-by-the-values-of-keys-in-another-array/#findComment-590229 Share on other sites More sharing options...
wildteen88 Posted July 15, 2008 Share Posted July 15, 2008 You might want to look into array_multisort instead. Link to comment https://forums.phpfreaks.com/topic/114621-solved-sort-an-array-by-the-values-of-keys-in-another-array/#findComment-590798 Share on other sites More sharing options...
Barand Posted July 15, 2008 Share Posted July 15, 2008 usinf usort() <?php function mysort($a, $b) { global $d; return array_search($a['id'], $d) - array_search($b['id'], $d); } usort($e, 'mysort'); echo '<pre>', print_r($e, true), '</pre>'; ?> Link to comment https://forums.phpfreaks.com/topic/114621-solved-sort-an-array-by-the-values-of-keys-in-another-array/#findComment-591003 Share on other sites More sharing options...
diamondnular Posted July 16, 2008 Author Share Posted July 16, 2008 usinf usort() <?php function mysort($a, $b) { global $d; return array_search($a['id'], $d) - array_search($b['id'], $d); } usort($e, 'mysort'); echo '<pre>', print_r($e, true), '</pre>'; ?> This code works! It takes generally half of the running time of my code. I think it is much more efficient than mine. Thanks Barand. D. Link to comment https://forums.phpfreaks.com/topic/114621-solved-sort-an-array-by-the-values-of-keys-in-another-array/#findComment-591810 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.