Type of PHP array-associative array

Source: Internet
Author: User
Tags scalar
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"

    1. Featured Topics: "PHP array"

    2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.