type of PHP array-associative array
What is a php associative array?
In the previous article we introduced the "type of PHP array-numeric index array", today we specifically introduced the next associative array.
In addition to array index arrays, PHP also has an associative array, in other computer languages, generally called hash or map
Using associative arrays, we can assign a keyword to each array element, which we call the key
$info = [ ' name ' = ' Andy ', ' age ' = ', ' gender ' = ' male '];
Equivalent to
$info = Array ( ' name ' = ' Andy ', ' age ' = ', ' gender ' = ' male ');
Associative arrays using numeric subscripts are unable to get data, for example $info[0] The value is empty, we need to use the key as subscript $info[' age ' value is 18.
The key name of an associative array can be a mixture of numbers and strings, unlike a numeric index array whose key name is only numeric. In an array, as long as there is not a number in the key name, the array is called an associative array.
Associative arrays, and arrays, consist of fields and methods that use names as keys.
It contains scalar data, which can be selected individually by index values, and the array's index value is not a nonnegative integer but an arbitrary scalar. These scalars are called keys and can be used later to retrieve the values in the array.
The elements of an associative array are not in a particular order, and you can think of them as a set of cards. The upper part of each card is indexed and the lower half is the value.
The object nature of JavaScript is an associative array.
The associative array (associative array) uses a string index (or key) to access the values of the individual elements stored in the array, with the key values shown in the following table. An array of associative indexes is useful for database-level interaction.
The associative array case is as follows:
<?php$newarray=array ("First" =>1, "second" =>2, "third" =>3); Echo $newarray ["second"]; $newarray ["Third"] =8;echo $newarray ["Third"];? >
The output is:
Tip: The key name of an associative array can make any integer or string. If the key name is a string, do not forget to add the delimiter-single quotation mark (') or double quotation mark (") to the key name or index. For numeric indexed arrays, we'd better add delimiters to avoid unnecessary hassles!
Next we explain the type of PHP array-multidimensional Arrays!
"Related tutorials Recommended"
Featured Topics: "PHP array"
Related Video courses recommended:
iterating over arrays with a For loop: indexes and associative arrays
iterating through arrays with while loops: indexes and associative arrays
traversing with a Foreach Loop: index and associative array