PHP's Array Extension in JavaScript (v1.1, 2017-03-03)
GD Productions | Support Forum
Creative Commons License

This JavaScript library lets you deal with arrays in a similar way than the PHP's array extension.

In PHP, you can use integers or strings to define the keys of an array. Here is an example.

$array1 = ["Hello", "World"]; // $array1[0] is "Hello", $array1[1] is "World"
$array2 = ["Hello" => 0, "World" => 1]; // $array2['Hello'] is 0, $array2['World'] is 1

In JavaScript, the situation is a bit different.

var array1 = ["Hello", "World"]; // array1[0] is "Hello", array1[1] is "World"
var array2 = {"Hello": 0, "World": 1}; // array2['Hello'] is 0, array2['World'] is 1

Have you noticed the differences?

It is possible to create sorts of associative arrays with this library, however arrays cannot contain integer and string keys at the same time, unlike the PHP's extension.

Getting started

Please wait...

Then upload the .js file to your server and embed it by using the <script> tag in your HTML code.

You are ready!

Notable changes

Constants

The following constants can be used in several functions.

The following constants can be used in specific functions only.

Standard functions

is_array ( mixed array [, constant key_type = ARRAY_KEY_ALL ] )
This function finds whether the given variable is an array.

This function returns true if array is an array, false otherwise.

var array = array_create(ARRAY_KEY_INT, "Hello", "World"); // creates an indexed array with 2 values
var check_array_1 = is_array(array); // returns true
var check_array_2 = is_array(array, ARRAY_KEY_INT); // returns true
var check_array_3 = is_array(array, ARRAY_KEY_STRING); // returns false ("array" is not an associative array)

array_create ( constant key_type [, mixed value_1 [, mixed value_n ] ] )
This function creates an indexed or an associative array.

This function returns an array on success, false on failure.

var array1 = array_create(ARRAY_KEY_INT); // creates an empty indexed array
var array2 = array_create(ARRAY_KEY_INT, "I am", 4, "years old"); // creates an indexed array with 3 values
var array3 = array_create(ARRAY_KEY_STRING); // creates an empty associative array
var array4 = array_create(ARRAY_KEY_STRING, ["lives", 3], ["health", 100]); // creates an associative array with 2 values
var array5 = array_create(ARRAY_KEY_STRING, ["lives", 3], ["health"]); // returns false ("health" is a key with no value assigned)

array_push ( array array , constant overwrite = ARRAY_KEY_OVERWRITE_NO [, mixed value_1 [, mixed value_n ] ] )
This function pushes one or more elements onto the end of an array.

This function returns an integer which is the number of elements that have been pushed, with overwritten keys not being counted. It can also return false on failure.

var array = array_create(ARRAY_KEY_STRING); // creates an empty associative array
array_push(array, ARRAY_KEY_OVERWRITE_YES, ["lives", 3], ["health", 100]); // returns 2
array_push(array, ARRAY_KEY_OVERWRITE_YES, ["health", 80], ["money", 500]); // returns 1 ("health" has been overwritten)
array_push(array, ARRAY_KEY_OVERWRITE_NO, ["items", 15], ["money", 50]); // returns false (fail, "money" cannot be overwritten)

array_set ( array array , int|string key , mixed value )
This function sets the value to a given key, in an array.

This function returns true on success, false on failure.

var array = array_create(ARRAY_KEY_INT, 10, 20); // creates an indexed array with 2 values
array_set(array, 2, 30); // returns true (array[2] is 30)
array_set(array, 4, 40); // returns true (array[4] is 40 and array[3] is undefined)
array_set(array, 0, 50); // returns true (array[0] is now 50)

array_unset ( array array [, int|string|constant key = ARRAY_KEY_UNSET_ALL ] )
This function unsets an entire array or a specific key only.

This function returns true on success, false on failure which is the case if nothing is unset.

var array = array_create(ARRAY_KEY_INT, 10, 20, 30, 40, 50); // creates an indexed array with 5 values
array_unset(array, 2); // returns true (array's length is now 4)
array_unset(array); // returns true (array's length is now 0)

array_get ( array array , int|string key )
This function returns the value for a given key, in an array.

This function returns a mixed type if the key exists or undefined if it does not.

var array = array_create(ARRAY_KEY_STRING, ["lives", 3], ["health", 100]); // creates an associative array with 2 values
var health = array_get(array, "health"); // returns 100
var money = array_get(array, "money"); // returns undefined

array_copy ( array array [, int|string start = 0 [, int|string end = -1 ] ] )
This function copies all or part of an array and returns it as a new array.

This function returns an array on success, false on failure.

var array = array_create(ARRAY_KEY_INT, 10, 20, 30, 40, 50); // creates an indexed array with 5 values
var new_array_1 = array_copy(array); // copies the entire array
var new_array_2 = array_copy(array, 2); // copies the first three values
var new_array_3 = array_copy(array, 3, 3); // copies the fourth value only

array_count ( array array [, constant mode = ARRAY_COUNT_NORMAL [, constant behavior = ARRAY_BEHAVIOR_JS ] ] )
This function counts all elements in an array.

This function returns an integer which is the number of elements in the array, or false on failure.

var array = array_create(ARRAY_KEY_INT, 10, 20, 30, [40, 50], 60); // creates an indexed array with 5 values
array_set(array, 10, 110); // sets index 10 of "array" to 110
var count_array_1 = array_count(array); // returns 11 
var count_array_2 = array_count(array, ARRAY_COUNT_RECURSIVE); // returns 12
var count_array_3 = array_count(array, ARRAY_COUNT_NORMAL, ARRAY_BEHAVIOR_PHP); // returns 6
var count_array_4 = array_count(array, ARRAY_COUNT_RECURSIVE, ARRAY_BEHAVIOR_PHP); // returns 7

Split and join functions

explode ( string string , string delimiter [, int limit = ARRAY_INTERNAL_MAX_INT [, constant behavior = ARRAY_BEHAVIOR_JS ] ] )
This function splits a string by string.

This function returns an indexed array on success, false on failure.

var array1 = explode("Hello World", " "); // returns ["Hello", "World"]
var array2 = explode("10|20|30|40|50", "|", 3); // returns ["10", "20", "30|40|50"]
var array3 = explode("10|20|30|40|50", "|", -3, ARRAY_BEHAVIOR_JS); // returns ["10|20|30", "40", "50"]
var array4 = explode("10|20|30|40|50", "|", -3, ARRAY_BEHAVIOR_PHP); // returns ["10", "20"]

implode ( array array [, string glue = "," ] )
This function joins array elements with a string.

This function returns a string on success, false on failure.

var array1 = array_create(ARRAY_KEY_INT, "I", "am", 4, "years", "old"); // creates an indexed array with 5 values
var string1 = implode(array1, " "); // returns "I am 4 years old"
var array2 = array_create(ARRAY_KEY_STRING, ["food", "pizza"], ["drink", "water"], ["money", 500]); // creates an associative array with 3 values
var string2 = implode(array2); // returns "pizza,water,500"

Key-related functions

array_key_exists ( array array , int|string key [, constant strict = ARRAY_CHECK_STRICT_YES ] )
This function checks if a given key exists in an array.

This function returns true on success, false on failure.

var array = array_create(ARRAY_KEY_INT, "Hello, "World"); // creates an indexed array with 2 values
array_set(array, 10, "Earth"); // sets index 10 of "array" to "Earth"
var key_exists_array_1 = array_key_exists(array, 0); // returns true
var key_exists_array_2 = array_key_exists(array, "10", ARRAY_CHECK_STRICT_NO); // returns true
var key_exists_array_3 = array_key_exists(array, 8); // returns false

array_keys ( array array [, mixed value [, constant strict = ARRAY_CHECK_STRICT_YES ] ] )
This function returns all the keys or a subset of the keys of an array.

This function returns an indexed array that can be empty if the specified value is not matched, or false on failure.

var array = array_create(ARRAY_KEY_STRING, ["firstname", "Bart"], ["lastname", "Simpson"], ["firstname_father", "Homer"], ["lastname_father", "Simpson"]); // creates an associative array with 4 values
var keys_array_1 = array_keys(array); // returns ["firstname", "lastname", "firstname_father", "lastname_father"]
var keys_array_2 = array_keys(array, "Simpson"); // returns ["lastname", "lastname_father"]
var keys_array_3 = array_keys(array, "Marge"); // returns []

Value-related functions

in_array ( array array , mixed value [, constant strict = ARRAY_CHECK_STRICT_YES ] )
This function checks if a value exists in an array.

This function returns true if value is found in array, false otherwise.

var array = array_create(ARRAY_KEY_STRING, ["food", "pizza"], ["drink", "water"], ["money", 500]); // creates an associative array with 3 values
var in_array_1 = in_array(array, "pizza"); // returns true
var in_array_2 = in_array(array, "500", ARRAY_CHECK_STRICT_NO); // returns true
var in_array_3 = in_array(array, "500"); // returns false ("500" as a string is not 500 as an integer)
var in_array_4 = in_array(array, "drink"); // returns false ("drink" is a key, not a value)

© 2017 GD Productions - Back to top