Php array definition tutorials

Source: Internet
Author: User
Tags array definition
In php, arrays can be defined in several ways, such as array () or arr, next I will give you a detailed introduction of various techniques for defining php arrays.

In php, array definition has several methods, such as array () or arr [], to implement array definition. next I will give you a detailed explanation of various techniques for defining php arrays.

PHP array is an important concept. it contains a large number of functions for people's development... Now we classify its arrays to facilitate query and application.

Let's talk about the definition of PHP array... The PHP array contains two items: key and value. you can use the key to obtain the corresponding value. The key can be a value and associated, for example, $ array [0]. $ array [one]…

Create an array

The array declaration in PHP is a little different from that in other languages, but it can also be declared as one-dimensional, two-dimensional, three-dimensional and multi-dimensional, such

$ Array [0] = 1, $ array = array (, 3); one-dimensional array, which contains only three values and belongs to a numeric array. $ array is available for reference, [0] indicates 1. indexes can be omitted when a value array is created.

The array () structure is used to create an array in PHP. the instance code is as follows:

$ People = array ('name', 'sex', 'nation', 'brith ');

To display the values of each element in the array, we use an index starting from 0. The index number is in square brackets after the variable name. The example code is as follows:

  1. $ People = array ('name', 'sex', 'nation', 'birth ');
  2. Echo $ people [2];
  3. ?>

The output $ people [2] indicates that nation is displayed (the first index item is counted from 0 ). in addition to numeric index arrays, PHP also supports related arrays. The so-called related arrays are non-intuitive replace with custom keywords.

Code:

  1. $ Doneles = array ('xm '=> 'name', 'xb' => 'sex', 'mz' => 'nation ', 'CS '=> 'birth ');
  2. Echo $ doneles ['CS '];
  3. ?>

The related array is used to make the output selection intuitive (no pre-calculation of index numbers is required and then output). the defined keywords and values are defined using the "=>" symbol.

Based on the two display methods of PHP array elements, you can also create numbers automatically without the need for array () declaration and initialization like Variables. the instance code is as follows:

  1. $ People [0] = 'name ';
  2. $ People [1] = 'sex ';
  3. $ People [2] = 'Nation ';
  4. $ People [3] = 'brith ';
  5. // Or
  6. // Copy the following code:
  7. $ Les ['xm '] = 'name ';
  8. $ Doneles ['xb '] = 'sex ';
  9. $ Doneles ['mz'] = 'Nation ';
  10. $ Les ['CS '] = 'birth ';

The size of the array varies dynamically based on the number of elements added. The instance code is as follows:

  1. // Index the array
  2. $ User [0] = 1; // user serial number
  3. $ User [1] = "zhangsan"; // user name
  4. $ User [2] = 10; // age
  5. $ User [3] = "nan"; // gender
  6. Echo'
    ';  
  7. Print_r ($ user );
  8. Echo'';
  9. // Associate an array
  10. $ User ["id"] = 1;
  11. $ User ["name"] = "zhangsan ";
  12. $ User ["age"] = 10;
  13. $ User ["sex"];
  14. $ User ["age"] = 90; // value assignment
  15. Echo $ user ["name"]; // output
  16. // Use array () to declare an array
  17. $ User = array (1, "zhangsan", 10, "nan ");
  18. // Declare the joined array using array ()
  19. $ User = array ("id" => 1, "name" => "zhangsan", "age" => 10, "sex" => "nan ");
  20. // Declare multi-dimensional arrays (multiple records) to save multiple user information records in a table
  21. $ User = array (
  22. // Call this line with $ user [0]. for example, call the name in this record, $ user [0] [1]
  23. Array (1, "zhangsan", 10, "nan "),
  24. // Call this line with $ user [1]. for example, call the name in this record, $ user [1] [1]
  25. Array (2, "lisi", 20, "nv ")
  26. );
  27. // Multiple tables are saved in an array. Each table has multiple records.
  28. $ Info = array (
  29. "User" => array (
  30. Array (1, "zhangsan", 10, "nan "),
  31. Array (2, "lisi", 20, "nv ")
  32. ),
  33. "Score" => array (
  34. Array ),
  35. Array (2, 60, 40, 70)
  36. )
  37. );
  38. Echo $ info ["score"] [1] [1]; // output 60,

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.