payment by upi: sinhamit@icici or payment by bank account name: amit kumar sinha, account number: 2646728782 IFSC code: KKBK0005660 SWIFT: KKBKINBB

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/double quotes, are evaluated as key of integer type.

<?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 []).

array(9) { [0]=> string(1) "c" [1]=> string(1) "d" ["+1"]=> string(1) "e" [-1]=> string(1) "g" ["01"]=> string(1) "h" ["0b1"]=> string(1) "i" ["0x1"]=> string(1) "j" ["true"]=> string(1) "k" ["1one"]=> string(1) "l" }

(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/double quotes) will be type-casted to 1 and 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) "hello" }
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 array
  • array_chunk − Splits an array into chunks
  • array_column − Returns the values from a single column in the input array
  • array_combine − Creates an array by using one array for keys and another for its values
  • array_count_values − Counts all the values of an array
  • array_diff_assoc − Computes the difference of arrays with additional index check
  • array_diff_key − Computes the difference of arrays using keys for comparison
  • array_diff_uassoc − Computes the difference of arrays with additional index check which is performed by a user supplied callback function
  • array_diff_ukey − Computes the difference of arrays using a callback function on the keys for comparison
  • array_diff − Computes the difference of arrays
  • array_fill_keys − Fills an array with values, specifying keys
  • array_fill − Fills an array with values
  • array_filter − Filters elements of an array using a callback function
  • array_flip − Exchanges all keys with their associated values in an array
  • array_intersect_assoc − Computes the intersection of arrays with additional index check
  • array_intersect_key − Computes the intersection of arrays using keys for comparison
  • array_intersect_uassoc − Computes the intersection of arrays with additional index check, compares indexes by a callback function
  • array_intersect_ukey − Computes the intersection of arrays using a callback function on the keys for comparison
  • array_intersect − Computes the intersection of arrays
  • array_is_list − Checks whether a given array is a list
  • array_key_exists − Checks if the given key or index exists in the array
  • array_key_first − Gets the first key of an array
  • array_key_last − Gets the last key of an array
  • array_keys − Returns all the keys or a subset of the keys of an array
  • array_map − Applies the callback to the elements of the given arrays
  • array_merge_recursive − Merges one or more arrays recursively
  • array_merge − Merges one or more arrays
  • array_multisort − Sorts multiple or multi-dimensional arrays
  • array_pad − Pads array to the specified length with a value
  • array_pop − Pops the element off the end of array
  • array_product − Calculates the product of values in an array
  • array_push − Pushes one or more elements onto the end of array
  • array_rand − Picks one or more random keys out of an array
  • array_reduce − Iteratively reduces the array to a single value using a callback function
  • array_replace_recursive − Replaces elements from passed arrays into the first array recursively
  • array_replace − Replaces elements from passed arrays into the first array
  • array_reverse − Returns an array with elements in reverse order
  • array_search − Searches the array for a given value and returns the first corresponding key if successful
  • array_shift − Shifts an element off the beginning of array
  • array_slice − Extracts a slice of the array
  • array_splice − Removes a portion of the array and replace it with something else
  • array_sum − Calculates the sum of values in an array
  • array_udiff_assoc − Computes the difference of arrays with additional index check, compares data by a callback function
  • array_udiff_uassoc − Computes the difference of arrays with additional index check, compares data and indexes by a callback function
  • array_udiff − Computes the difference of arrays by using a callback function for data comparison
  • array_uintersect_assoc − Computes the intersection of arrays with additional index check, compares data by a callback function
  • array_uintersect_uassoc − Computes the intersection of arrays with additional index check, compares data and indexes by separate callback functions
  • array_uintersect − Computes the intersection of arrays, compares data by a callback function
  • array_unique − Removes duplicate values from an array
  • array_unshift − Prepends one or more elements to the beginning of an array
  • array_values − Returns all the values of an array
  • array_walk_recursive − Applies a user function recursively to every member of an array
  • array_walk − Applies a user supplied function to every member of an array
  • array − Creates an array
  • arsort − Sorts an array in descending order and maintain index association
  • asort − Sorts an array in ascending order and maintain index association
  • compact − Creates array containing variables and their values
  • count − Counts all elements in an array or in a Countable object
  • current − Returns the current element in an array
  • each − Returns the current key and value pair from an array and advance the array cursor
  • end − Sets the internal pointer of an array to its last element
  • extract − Imports variables into the current symbol table from an array
  • in_array − Checks if a value exists in an array
  • key_exists − Alias of array_key_exists function
  • key − Fetches a key from an array
  • krsort − Sorts an array by key in descending order
  • ksort − Sorts an array by key in ascending order
  • list − Assigns variables as if they were an array
  • natcasesort − Sorts an array using a case insensitive "natural order" algorithm
  • natsort − Sorts an array using a "natural order" algorithm
  • next − Advances the internal pointer of an array
  • pos − Alias of current function
  • prev − Rewinds the internal array pointer
  • range − Creates an array containing a range of elements
  • reset − Sets the internal pointer of an array to its first element
  • rsort − Sorts an array in descending order
  • shuffle − Shuffles an array
  • sizeof − Alias of count function
  • sort − Sorts an array in ascending order
  • uasort − Sorts an array with a user-defined comparison function and maintain index association
  • uksort − Sorts an array by keys using a user-defined comparison function
  • usort − Sorts an array by values using a user-defined comparison function

Leave a Comment: