Php array usage summary, php array summary _ PHP Tutorial

Source: Internet
Author: User
Php array usage summary, php array summary. Php array use summary, php array summary in PHP, arrays are divided into two types: index array and associated array. The two can be used separately or in combination. 1. summary of the use of one-dimensional array php array and php array

In PHP, arrays are divided into two types: index arrays and associated arrays. The two can be used separately or in combination.

1. one-dimensional array

The definition of one-dimensional arrays is also very simple. There are two common methods:

1.1 Direct assignment

1: 3: $ dwqs [1] = "my blog :";
5: $ dwqs [3] = "programmers :";
7: $ dwqs [5] = "welcome to join ";
1.2 Array () build an Array

1: 3:?>

2. multi-dimensional array

Take the joined array as an example

1: 3: "number" => array (1, 2, 3 ),
Array ("Independent blog", "CSDN", "blog Garden "),
array("www.ido321.com","blog.csdn.net/u011043843","www.cnblogs.com/ido321")  
 
2. array traversal in PHP, there are three common array traversal methods: 1. for Loop 1: 3: echo "$ dwqs [I]
";
2. foreach statement

1: // method 1
   3: foreach($dwqs as $value){
;
   6:  
8:
 
  10:     echo $key."=>".$value;
3. while loop

1: 3: echo $ key. ":". $ value;
 

3. some array-related functions (use the print_r () function to output the array content)

PHP arrays are very powerful and are one of the most commonly used data types. Its processing functions are also powerful and efficient.

1. array key/value operation functions

1.1 function array_values (): returns the values of all elements in the array. You can pass in the array name without retaining the key name. The Returned array starts from 0 and then re-creates the index.

1: "Blog" => "www.ido321.com", "programmers" => "QQ group: 259280570 ");
   4: print_r(array_values($dwqs2));
"" Www.ido321.com "," programmers "="> "" QQ group: 259280570 "); <=" "font =" ">
   7:  
1.2 function array_keys (): return the key name in the array.

1: 3: print_r (array_keys ($ dwqs2 ))
   5: print_r(array_kays($dqws,'ID'));
1.3 function In_array (): checks whether a value exists in the array.

1: 3: // output: Exists
5: echo "exists ";
   7: else{
   9: }
 
2. number of arrays and uniqueness 2.1 function count (): counts the number of elements in an array or the number of attributes in an object. 1: 3:?>

2.2 function array_unique (): deletes repeated values in the array, and the returned array key name remains unchanged.

1: 'Php', 'B' => 'mysql', 'C' => 'Linux ', 'D' => 'php ');
   4: print_r(array_unique($a));
2.3 function array_count_values (): counts the number of occurrences of all values in the array. the returned array uses the value in the original array as the key name, and the key value is the number of occurrences of the element in the original array.

1: 3: print_r (array_count_values ($ ));
 
3. array sorting


Source: http://www.ido321.com/596.html

How to use arrays in php classes?

If you define private $ a; as public $ a, you can directly use $ obj->.

$ Obj-> a ['key'] is the value you want to access.

Php array usage

First of all, your writing method is really different. now PHP is more generic.

(1) PHP, a weak language, does not need to be initialized like a rigorous language. it is automatically initialized when used. Therefore, the array can be used after it is declared. In addition, sometimes it does not need to be declared, such as writing $ I = 1.25 directly. If an error is reported in a rigorous language, PHP can do this.
(2) the default index of the array starts from 0 and then increases from 1. Count ($ arr) is the number of array elements. If n is the number of elements in the current array, $ arr [n-1] is the largest element in the index. Adding $ arr [n] is equivalent to adding an element behind the array. In fact, you can write $ arr [] = $ ss, with the same effect.
(3) an algorithm concept problem means that a natural number can be divisible by a prime number smaller than it, but it is not a prime number.
(4) no program error ....

In PHP, arrays are divided into two types: index arrays and associated arrays. The two can be used separately or in combination. 1. one-dimensional 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.