Skip to content

Instantly share code, notes, and snippets.

@revathskumar
Last active December 20, 2015 15:59
Show Gist options
  • Save revathskumar/6158163 to your computer and use it in GitHub Desktop.
Save revathskumar/6158163 to your computer and use it in GitHub Desktop.
PHP : Add element in the beginning of an array
<?php
$a = array(
1 => "First",
2 => "Second",
5 => "Fifth"
);
?>
<?php
array_unshift($a, "Zeroth");
print_r($a);
// array(
// 0 => "Zeroth",
// 1 => "First",
// 2 => "Second",
// 3 => "Fifth"
// );
?>
<?php
$b = array("Zeroth") + $a;
print_r($b);
// array(
// 0 => "Zeroth",
// 1 => "First",
// 2 => "Second",
// 5 => "Fifth"
// );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment