Skip to content

Instantly share code, notes, and snippets.

@iampaul83
Created April 30, 2015 12:00
Show Gist options
  • Save iampaul83/c654bf6cae043cdc286b to your computer and use it in GitHub Desktop.
Save iampaul83/c654bf6cae043cdc286b to your computer and use it in GitHub Desktop.
<style>
body > div {
padding: 10px;
border-style: solid;
border-width: 1px;
border-color: black;
}
.array {
margin: 5px;
padding: 10px;
background-color: rgb(221, 221, 221);
}
.array_element {
padding-left: 50px;
}
.take {
margin: 10px;
color: red;
}
</style>
<?php
//pretty print array
function array_print($name, $array) {
echo "<div class='array'>$name<br>(<br><div class='array_element'>";
foreach ($array as $key => $value) {
echo "($key) -> $value,<br>";//$key . $value . ",<br>";
}
echo "</div>)</div>";
}
//---------------------------------------------------------------------------------------
echo "<h1>方法一</h1><div>";
//---------------------------------------------------------------------------------------
function array_take(&$array, $index) {
$value = $array[$index];
$array = array_merge( array_slice($array, 0, $index), array_slice($array, $index + 1) );
return $value;
}
$array = array(1,2,3,4,5);
$index = 3;
array_print("原始array",$array);
echo "<div class='take'>take array[$index] -> " . array_take($array, $index) . "</div>";
array_print("array",$array);
//---------------------------------------------------------------------------------------
echo "</div><h1>方法二</h1><div>";
//---------------------------------------------------------------------------------------
function array_take_2(&$array, $index) {
$value = $array[$index];
unset($array[$index]);
$array = array_values($array);
return $value;
}
$array = array(1,2,3,4,5);
$index = 3;
array_print("原始array",$array);
echo "<div class='take'>take array[$index] -> " . array_take_2($array, $index) . "</div>";
array_print("array",$array);
echo "</div>";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment