kreut Posted February 18, 2011 Share Posted February 18, 2011 Hello, I have an array, $solutions, and a separate indexing array, $solution_order. From the $solution_order array (developed from the $order variable, where each $order variable looks like '0321' or '3021'), I'd like to create a new array $ordered_solutions, which indexes on the values picked from the $solution_order array. For example, if my $solutions array is (Big, Small, Fat, Skinny) and my $solution_order array is (2,3,1,0), then the final results should be (Fat, Skinny, Small, Big). I've written down what I have so far. Any thoughts about how to move this forward would be appreciated. Thanks! function GetOrder($solution, $wrong1, $wrong2, $wrong3, $order) { $solutions = array( $solution, $wrong1, $wrong2, $wrong3); $solution_order = array(substr($order,0,1), substr($order,1,1), substr($order,2,1), substr($order,3,1)); $ordered_solutions = array(); //what do to here?? return $ordered_solutions; } Link to comment https://forums.phpfreaks.com/topic/228105-more-fun-with-arrays/ Share on other sites More sharing options...
requinix Posted February 18, 2011 Share Posted February 18, 2011 There's a function for that. function GetOrder($solution, $wrong1, $wrong2, $wrong3, $order) { $ordered = array_combine(str_split($order), array($solution, $wrong1, $wrong2, $wrong3)); ksort($ordered); // optional. depends on your code return $ordered; } Link to comment https://forums.phpfreaks.com/topic/228105-more-fun-with-arrays/#findComment-1176339 Share on other sites More sharing options...
kreut Posted February 18, 2011 Author Share Posted February 18, 2011 Very clear solution! Thank you (and thanks PHP).... Link to comment https://forums.phpfreaks.com/topic/228105-more-fun-with-arrays/#findComment-1176494 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.