PHP array assignment method

Source: Internet
Author: User
This article mainly introduces how to assign values to PHP arrays. This article describes how to create arrays and how to assign values to PHP arrays in detail, hoping to help you. PHP arrays are still commonly used. so I studied the assignment of PHP arrays and shared them here. I hope they will be useful to you.

An array is a set of variables stored in computer memory. These variables can be of different types, including integers, boolean values, and strings. It can be said that arrays are a small database in the memory, which provides us with a quick way to access data-saving frequent access to IO or databases to improve performance, therefore, there are many operations on arrays.
You can use the following method to create an array in PHP:

To create an array:

<?php $a="abcd"; print($a[0]." ".$a[1]." ".$a[2]." ".$a[3]." "); ?> 

Result:A B c d

Method 2:

<?php $http=array("www","helpphp","cn");  print($http[0].".".$http[1].".".$http[2]);  ?> 

Knowledge description:

1. we can see from the previous example that, by default, the array in PHP marks the position of the variable in the array starting from the subscript (index), that is, we can use: $ access the created array using the variable name [index.

2. we use the array () language structure to create data. if the subscript is not explicitly specified in the array, the index is incremented from 0 by default. The array we created has been saved in the memory, but after using the data, we need to clear it to ensure memory leakage.

How to assign values to PHP arrays? As follows:

$My_array=array(); $My_array[]=”www” $My_array[]=”helpphp”; $My_array[]=”cn”; 

Using the above method, I created an empty array for the variable using the array language structure, and assigned a value for $ My_array in the subsequent statements. in the previous section, we know that, array subscript (index), auto increment from 0, that is, the following statement will output helpphp.
Print $ My_array [1];
If you want to output $ My_array at a time, you can use print_r ($ My_array ). to complete the preceding task, you only need to use the unset () function to destroy the elements in an array or array, as shown in the following code:
Unset ($ My_array [0];
Unset ($ My_array );
It should be noted that after the array elements are deleted, the index value will retain the original position and the index will not be re-adjusted. you only need to compare the deleted value to find this phenomenon. You can use var_dump () to view more details in the array, as shown below:
Var_dump ($ My_array );

The above is to teach you how to assign values to PHP arrays, hoping to inspire you to learn.

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.