PHP's Array Extension in JavaScript (v1.0, 2017-02-23)
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!

Constants

The following constants can be used in several functions.

ARRAY_BEHAVIOR_JS
This constant is useful in functions that have two implementations. This one lets you use a behavior specific to this library.

ARRAY_BEHAVIOR_PHP
This constant is useful in functions that have two implementations. This one lets you use the PHP standard behavior.

ARRAY_KEY_ALL
This constant is useful to check if a value is an indexed or an associative array, indifferently.

ARRAY_KEY_INT
This constant is useful to create or check if a value is an indexed array (integer keys).

ARRAY_KEY_STRING
This constant is useful to create or check if a value is an associative array (string keys).

ARRAY_CHECK_STRICT_YES
This constant is useful to check if the types between two values are the same.

ARRAY_CHECK_STRICT_NO
This constant is useful to not take into account the types when comparing two values.

The following constants can be used in specific functions only.

ARRAY_KEY_OVERWRITE_YES
This constant is useful to allow the keys to be overwritten, when using array_push.

ARRAY_KEY_OVERWRITE_NO
This constant is useful to prevent the keys to be overwritten, when using array_push.

ARRAY_KEY_UNSET_ALL
This constant is useful to unset an entire array, when using array_unset.

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)

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

This function returns true if value is found in the 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"); // returns true
var in_array_3 = in_array(array, "500", ARRAY_CHECK_STRICT_YES); // 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)

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 when successful or false if an error has occurred.

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 , const 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 the number of elements that have been pushed, with overwritten keys not being counted. It can also return false if an error has occurred.

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 when successful or false if an error has occurred.

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 when successful or false if nothing has been unset or an error has occurred.

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 start = 0 , int end = -1 ] )
This function copies all or part of an array and returns it as a new array.

This function returns an array or false if an error has occurred.

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

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 when successful or false if an error has occurred.

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

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

This function returns a string when successful or false if an error has occurred.

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"

© 2017 GD Productions - Back to top