Jump to content

More Fun with arrays


kreut

Recommended Posts

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

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;
}

Archived

This topic is now archived and is closed to further replies.

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