Mining PHP array function library array creation and use

Source: Internet
Author: User

The function library in the array is the main point for us to learn about arrays. Here we will mainly introduce the PHP array function library array to create an array. Let's share it with everyone. Fortunately, PHP is not difficult, and there are not many such things. After learning a few more lessons, we can start to use it to implement the functions of a website. I believe that you will become more and more energetic. Okay. Start today's course. Today, we are studying arrays. arrays are a type of data that is frequently used. Learning to process arrays will make your website easy.

For example, your website database contains a large article. Now you want to display 20 titles of entertainment news on a page, do you want to use 20 variables after these 20 titles are extracted from the database? I think you will not be so stupid, so what variables can be used to fully receive the 20 pieces of data? This requires array variables. What is an array? An array is actually a data set, which is equivalent to a data container. A lot of data is stored in it. We can store or retrieve it in a certain way, you can also sort the data in it, and check whether there is any data we want.

PHP array function library new array use array

 
 
  1. array array ( [mixed ...] ) 

Returns an array created based on parameters. You can use the => operator to provide the index for the parameter. For information about what an array is, read the array section. Note: array () is a language structure used to represent arrays literally, rather than regular functions. The syntax "index => values" is separated by commas (,), which defines the index and value. The index can be a string or number. If the index is omitted, an integer index starting from 0 is automatically generated. If the index is an integer, the next index will be the currently Largest Integer index + 1. Note that if two identical indexes are defined, the next one will overwrite the previous one. Although it is uncommon to add a comma after the last defined array item, it is a legal syntax.

The following example demonstrates how to create a two-dimensional array, how to specify a key name for the corresponding array, and how to slightly over and continue the Numerical index in a common array.

PHP array function library example 1. array () Example

 
 
  1. <?php 
  2. $fruits = array (  
  3. "fruits"  => array("a" => "orange", "b" => "banana", "c" => "apple"),  
  4. "numbers" => array(1, 2, 3, 4, 5, 6),  
  5. "holes"   => array("first", 5 => "second", "third")  
  6. );  
  7. ?> 

PHP array function library example 2. Automatic Index of array ()

 
 
  1. <?php 
  2. $arrayarray = array(1, 1, 1, 1,  1, 8 => 1,  4 => 1, 19, 3 => 13);  
  3. print_r($array);  
  4. ?> 

The above example will output:

 
 
  1. Array  
  2. (  
  3. [0] => 1  
  4. [1] => 1  
  5. [2] => 1  
  6. [3] => 13  
  7. [4] => 1  
  8. [8] => 1  
  9. [9] => 19  

Note that index 3 is defined twice and the last value is 13. Index 4 is defined after index 8. The next automatically generated index (the value is 19) is 9, because the maximum index is 8.


Related Article

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.