Simple and Convenient PHP array assignment methods

Source: Internet
Author: User

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. array is a small database in memory. it provides a quick way for us to access data-it can save the IO or frequent access to the database to improve some performance, so there are many operations on the array.

You can use one of the following methods to create an array in PHP:

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

Result: a B c d

Method 2:

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

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 no subscript is 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, I believe this will be involved in subsequent chapters. Description: PHP array assignment and destruction. If you haven't learned: PHP array, you should first learn about it.

How to assign values to PHP arrays? As follows:

 
 
  1. $My_array=array();  
  2.  
  3. $My_array[]=”www”  
  4.  
  5. $My_array[]=”helpphp”;  
  6.  
  7. $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.

 
 
  1. 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:

 
 
  1. unset($My_array[0];  
  2.  
  3. 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:

 
 
  1. Var_dump($My_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.