0% found this document useful (0 votes)
446 views

Array Cheat Sheet

This document provides a cheat sheet for working with arrays in PHP and JavaScript. It outlines various methods for declaring and manipulating arrays, including: 1) Different ways to declare empty and populated arrays, such as using the array() function or [] syntax. 2) Methods for adding or removing elements from arrays, including at the end or specific indices. 3) How to work with associative arrays and objects in PHP and JavaScript. 4) Functions for getting the length of an array or checking the number of elements. The cheat sheet acts as a quick reference for common array operations in PHP and JavaScript.

Uploaded by

Qaiser Khan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
446 views

Array Cheat Sheet

This document provides a cheat sheet for working with arrays in PHP and JavaScript. It outlines various methods for declaring and manipulating arrays, including: 1) Different ways to declare empty and populated arrays, such as using the array() function or [] syntax. 2) Methods for adding or removing elements from arrays, including at the end or specific indices. 3) How to work with associative arrays and objects in PHP and JavaScript. 4) Functions for getting the length of an array or checking the number of elements. The cheat sheet acts as a quick reference for common array operations in PHP and JavaScript.

Uploaded by

Qaiser Khan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Array Cheat Sheet (PHP/JavaScript)

PHP
Declare empty array Declare array with size A) $myArray = array("apple", "green"); B) $myArray = array( 0 => "apple", 1 => "green" ); C) $myArray[] = "apple"; $myArray[] = "green"; $myArray = array( 0 => "apple", 3 => "green" ); A) array_push($myArray, "cat"); B) $myArray[] = "cat"; $myArray[2] = "cat"; A) unset($myArray[2]); B) array_splice($myArray, 2, 1); count($myArray); Returns actual number of elements $myArray = array( "fruit" => "apple", "color" => "green" ); A) myArray.push("cat"); B) myArray[myArray.length] = "cat"; myArray[2] = "cat"; myArray.splice(2, 1); myArray.length; Returns highest numerical index + 1 myArray = { "fruit": "apple", "color": "green" }; Actually creates an object with properties "fruit" and "color". myArray.length will return 0. myArray["animal"] = "cat"; Actually adds an object property "animal". $myArray = array();

JavaScript
A) myArray = new Array(); B) myArray = []; myArray = new Array(23); A) myArray = new Array("apple", "green"); B) myArray = ["apple", "green"];

Declare array with elements

Declare sparse array with elements Add element at the end Add element at index 2 Remove element at index 2 Array length

Declare associative array with elements

Add element to associative array

$myArray["animal"] = "cat";

Array Cheat Sheet (PHP/JavaScript), Version 1.1 by Stefan Pauwels, 2011. http://www.stefan.pauwels.ch/array_cheat_sheet.pdf

You might also like