Created
April 17, 2014 00:22
-
-
Save 0x4445565A/10944448 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function cardDance() { | |
$cards = array( | |
'rpid00001' => array( | |
'delta' => 1, | |
'status' => 'A', | |
), | |
'rpid00002' => array( | |
'delta' => 2, | |
'status' => 'B', | |
), | |
'rpid00003' => array( | |
'delta' => 3, | |
'status' => 'B', | |
), | |
'rpid00004' => array( | |
'delta' => 4, | |
'status' => 'A', | |
), | |
'rpid00005' => array( | |
'delta' => 5, | |
'status' => 'B', | |
), | |
); | |
moveCards($cards); | |
print_r($cards); | |
} | |
function moveCards(&$rpid_cards) { | |
$cards = array(); | |
$max_delta = count($rpid_cards); | |
// reorganize cards by delta | |
foreach ($rpid_cards as $rpid => $card) { | |
$delta = $card['delta']; | |
$cards[$delta] = $card; | |
$cards[$delta]['rpid'] = $rpid; | |
} | |
// Sort by delta | |
sortRecursive($cards); | |
// update array | |
foreach ($cards as $delta => $card) { | |
$rpid_cards[$card['rpid']]['delta'] = $delta; | |
} | |
} | |
function sortRecursive(&$cards) { | |
foreach ($cards as $delta => $card) { | |
if (isset($cards[$delta + 1])) { | |
$below = $cards[$delta + 1]; | |
if ($below['status'] == 'B' && $card['status'] != 'B') { | |
$cards[$delta + 1] = $card; | |
$cards[$delta] = $below; | |
return sortRecursive($cards); | |
} | |
} | |
} | |
return $cards; | |
} | |
cardDance(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment