Please support if you like my work by payment through upi: sinhamit@icici or payment by bank
account name: Amit Kumar Sinha,
account number: 2646728782
IFSC code: KKBK0005660
SWIFT: KKBKINBB
Array Keys and Elements in Category: PHP by amit
🕙 Posted on 2023-05-19 at 06:45:51 Read in Hindi ...
More on Array Keys
In previous page, you have learned that keys in an array can be of different data-types. However, you should note following rule of type-casting, when a key is different than integer or string data-type. When two or more keys in an array are evaluated as same integer or string value, then the last item of that evaluated key, will be counted.
(1) A array key of string data-type, which can be type-casted to integer, except which are prefixed with + (plus symbol) inside the pair of single/
<?php
$my_var = array
(
'a', /* numeric index, 0 */
'b', /* numeric index, 1 */
'0'
=> 'c', /* type-casted to 0 */
'1'
=> 'd', /* type-casted to 1 */
'+1' => 'e',
-1 => 'f', /* -1 is not equal to 1, and it is specifically provided here. -1 is also not the last key in this array. */
'-1' => 'g', /* type-casted to -1 */
'01' => 'h',
'0b1' => 'i',
'0x1' => 'j',
'true' => 'k',
'1one' => 'l',
);
var_dump( $my_var );?>
Though, there are twelve items in the array literal, assigned to $my_var in above example, but when they are processed by the PHP interpreter, only nine are displayed in the web-browser. The first item 'a' is replaced by 'c' and the second item 'b' is replaced by 'd'. The sixth item 'f' is replaced by 'g'. However, without the pair of single quotes, 0b1
, 0x1
, and true
are evaluated as 1. Except '0b1', '0x1' and 'true' all other string literals mentioned in above example, when type-casted with (int)
reserved keyword, they are evaluated as 1 (outside array parentheses array()
and array square brackets []
).
(2) Array keys of float point numbers are also type-casted to integers, which means that the fractional part (after the decimal point), will be truncated.
(3) Boolean keys are type-casted to integers, that is, true
(without a pair of single/false
will be, similarly, type-casted to 0 (zero digit).
(4) null
can be an array key, which will be type-casted to ""
(empty string, without space), and value assigned to either of these keys will replace the former. In the following example, the first item is replaced by the second item, in each of array literals:
<?php
var_dump( [ null
=> 5, "" => 'hello' ] );
echo '<br />';
var_dump( [ "" => 'good', null
=> 'world', ] );?>
array(1) { [""]=> string(5) "world" }
(5) The keys in an array cannot be of array data-type, or object data-type. These will show ERROR as Illegal offset type.
Following rules applied on array keys and array items (elements) are already explained in previous page:
When multiple items (elements) are added either in the array declaration, or manipulated afterwards which are associated with same key (evaluated), only the item associated with that key (used in the last), will be used as all other items are overwritten.
<?php
$new_var = array
(
'good', /* numeric index, 0 */
'morning', /* numeric index, 1 */
'1' => 'afternoon',
1.4 => 'evening',
true => 'night'
);
var_dump( $new_var ); ?>
PHP arrays can contain integer and string keys at the same time as PHP does not distinguish between indexed and associative arrays.
The key is optional. If it is not specified, PHP will use the increment of the largest previously used integer
key.
It is possible to specify the key only for some elements and leave it out for others.
Caution
: Prior to PHP 8.0.0, square brackets and curly braces could be used interchangeably for accessing array elements (e.g. $new_array[4] and $new_array{4} would both do the same thing). The curly brace syntax was deprecated as of PHP 7.4.0 and no longer supported as of PHP 8.0.0.
Array Functions
Array keys and items (or elements) can be manipulated with pre-defined array functions. You have already seen pre-defined array()
and count()
functions. A list of all array functions are mentioned hereinbelow. Some of useful PHP functions are explained in next page.
array_change_key_case
− Changes the case of all keys in an arrayarray_chunk
− Splits an array into chunksarray_column
− Returns the values from a single column in the input arrayarray_combine
− Creates an array by using one array for keys and another for its valuesarray_count_values
− Counts all the values of an arrayarray_diff_assoc
− Computes the difference of arrays with additional index checkarray_diff_key
− Computes the difference of arrays using keys for comparisonarray_diff_uassoc
− Computes the difference of arrays with additional index check which is performed by a user supplied callback functionarray_diff_ukey
− Computes the difference of arrays using a callback function on the keys for comparisonarray_diff
− Computes the difference of arraysarray_fill_keys
− Fills an array with values, specifying keysarray_fill
− Fills an array with valuesarray_filter
− Filters elements of an array using a callback functionarray_flip
− Exchanges all keys with their associated values in an arrayarray_intersect_assoc
− Computes the intersection of arrays with additional index checkarray_intersect_key
− Computes the intersection of arrays using keys for comparisonarray_intersect_uassoc
− Computes the intersection of arrays with additional index check, compares indexes by a callback functionarray_intersect_ukey
− Computes the intersection of arrays using a callback function on the keys for comparisonarray_intersect
− Computes the intersection of arraysarray_is_list
− Checks whether a given array is a listarray_key_exists
− Checks if the given key or index exists in the arrayarray_key_first
− Gets the first key of an arrayarray_key_last
− Gets the last key of an arrayarray_keys
− Returns all the keys or a subset of the keys of an arrayarray_map
− Applies the callback to the elements of the given arraysarray_merge_recursive
− Merges one or more arrays recursivelyarray_merge
− Merges one or more arraysarray_multisort
− Sorts multiple or multi-dimensional arraysarray_pad
− Pads array to the specified length with a valuearray_pop
− Pops the element off the end of arrayarray_product
− Calculates the product of values in an arrayarray_push
− Pushes one or more elements onto the end of arrayarray_rand
− Picks one or more random keys out of an arrayarray_reduce
− Iteratively reduces the array to a single value using a callback functionarray_replace_recursive
− Replaces elements from passed arrays into the first array recursivelyarray_replace
− Replaces elements from passed arrays into the first arrayarray_reverse
− Returns an array with elements in reverse orderarray_search
− Searches the array for a given value and returns the first corresponding key if successfularray_shift
− Shifts an element off the beginning of arrayarray_slice
− Extracts a slice of the arrayarray_splice
− Removes a portion of the array and replace it with something elsearray_sum
− Calculates the sum of values in an arrayarray_udiff_assoc
− Computes the difference of arrays with additional index check, compares data by a callback functionarray_udiff_uassoc
− Computes the difference of arrays with additional index check, compares data and indexes by a callback functionarray_udiff
− Computes the difference of arrays by using a callback function for data comparisonarray_uintersect_assoc
− Computes the intersection of arrays with additional index check, compares data by a callback functionarray_uintersect_uassoc
− Computes the intersection of arrays with additional index check, compares data and indexes by separate callback functionsarray_uintersect
− Computes the intersection of arrays, compares data by a callback functionarray_unique
− Removes duplicate values from an arrayarray_unshift
− Prepends one or more elements to the beginning of an arrayarray_values
− Returns all the values of an arrayarray_walk_recursive
− Applies a user function recursively to every member of an arrayarray_walk
− Applies a user supplied function to every member of an arrayarray
− Creates an arrayarsort
− Sorts an array in descending order and maintain index associationasort
− Sorts an array in ascending order and maintain index associationcompact
− Creates array containing variables and their valuescount
− Counts all elements in an array or in a Countable objectcurrent
− Returns the current element in an arrayeach
− Returns the current key and value pair from an array and advance the array cursorend
− Sets the internal pointer of an array to its last elementextract
− Imports variables into the current symbol table from an arrayin_array
− Checks if a value exists in an arraykey_exists
− Alias of array_key_exists functionkey
− Fetches a key from an arraykrsort
− Sorts an array by key in descending orderksort
− Sorts an array by key in ascending orderlist
− Assigns variables as if they were an arraynatcasesort
− Sorts an array using a case insensitive "natural order" algorithmnatsort
− Sorts an array using a "natural order" algorithmnext
− Advances the internal pointer of an arraypos
− Alias of current functionprev
− Rewinds the internal array pointerrange
− Creates an array containing a range of elementsreset
− Sets the internal pointer of an array to its first elementrsort
− Sorts an array in descending ordershuffle
− Shuffles an arraysizeof
− Alias of count functionsort
− Sorts an array in ascending orderuasort
− Sorts an array with a user-defined comparison function and maintain index associationuksort
− Sorts an array by keys using a user-defined comparison functionusort
− Sorts an array by values using a user-defined comparison function
Leave a Comment:
Amit Sinha March 2nd, 2023 at 9:30 PM
😃 😄 😁 😆 😅 😂 😉 😊 😇 😍 😘 😚 😋 😜 😝 😶 😏 😒 😌 😔 😪 😷 😵 😎 😲 😳 😨 😰 😥 😢 😭 😱 😖 😣 😞 😓 😩 😫 😤
Ribhu March 3rd, 2023 at 9:30 PM
🐵 🐒 🐶 🐩 🐺 🐱 🐯 🐅 🐆 🐴 🐎 🐮 🐂 🐃 🐄 🐷 🐖 🐗 🐽 🐏 🐑 🐐 🐪 🐫 🐘 🐭 🐁 🐀 🐹 🐰 🐇