Back to home

PushPopShiftUnshiftExample: Basic Tree ImplementationResources + Extra Reading
PHP Array Method Applications main image

PHP Array Method Applications

This covers the basic actions of applying a push, pop, shift and unshift function with PHP.

These functions are incredibly useful for data structure implementations.

Push

Using array_push:

1 2 3 4 // create our array with 1 element $arr = array("one"); // $count will be 3 and $arr will now be array("one","two","three"); $count = array_push($arr,"two","three");

Alternative using $arr[]:

1 2 3 4 5 6 7 8 // create our array with 1 element $arr = array("one"); // $arr will now be array("one","two"); $arr[] = "two"; // $arr will now be array("one","two","three"); $arr[] = "three"; // $count will be 3 $count = count($arr)

Alternative using $array_merge():

1 2 3 4 5 // create our array with 1 element $arr = array("one"); // alternate method using array_merge() $arr = array_merge($arr,array("two","three")); // $arr will now be array("one","two","three"); $count = count($arr); // $count will be 3

Pop

1 2 3 4 // create our array with 3 elements $arr = array("one","two","three"); // $value will be "three" and array's value will now be array("one","two"); $value = array_pop($arr);

Shift

1 2 3 4 // create our array with 3 elements $arr = array("one","two","three"); // $value will be "one" and array's value will now be array("two","three"); $value = array_shift($arr);

Unshift

1 2 3 4 // create our array with 3 elements $arr = array("three","four","five"); // $count will now be 5 and array will hold one - five $count = array_unshift($arr,"one","two");

Example: Basic Tree Implementation

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 <?php class Tree { function __construct($root = null) { $this->root = $root; } public function bfs() { // 1. shift val // 2. if children, append to arr // 3. append data to array to compare if ($this->root == null) { throw new Error("No tree root"); } $arr = array($this->root); $res = []; while (count($arr) > 0) { $x = array_shift($arr); if ($x->children != null) { $arr = array_merge($arr, $x->children); } array_push($res, $x->data); } return $res; } public function dfs() { // shift from arr // if children exist, unshift array // add data to res array $res = []; $arr = array($this->root); while (count($arr) > 0) { $x = array_shift($arr); if ($x->children != null) { $arr = array_merge($x->children, $arr); } array_push($res, $x->data); } return $res; } } class Node { function __construct($data = null, $children = null) { $this->data = $data; $this->children = $children; } }

Resources + Extra Reading

http://www.thecave.info/php-array-push-pop-shift-and-unshift/

Dennis O'Keeffe

@dennisokeeffe92
  • Melbourne, Australia

Hi, I am a professional Software Engineer. Formerly of Culture Amp, UsabilityHub, Present Company and NightGuru.
I am currently working on workingoutloud.dev, Den Dribbles and LandPad .

Related articles


1,200+ PEOPLE ALREADY JOINED ❤️️

Get fresh posts + news direct to your inbox.

No spam. We only send you relevant content.