PHP Array Usage Summary

Source: Internet
Author: User

In PHP, arrays are divided into two categories: indexed arrays and associative arrays. They can be used alone or in combination.

1. One-dimensional arrays

The definition of one-dimensional array is also very simple, commonly used in the following two ways:

1.1 Direct Assignment

1:<?php
   3:  $dwqs [1] = "My blog:"; 
   5:  $DWQS [3] = "Program enthusiast:"; 
   7:  $dwqs [5] = "Welcome to join";
1.2 Array () build arrays

1:<?php
   3:  ?>

2. Multidimensional arrays

Take an associative array as an example

1:<?php
   3:     "numbered"array,
Array ("Independent blog","CSDN","blog Park"),
Array ("www.ido321.com","blog.csdn.net/u011043843","www.cnblogs.com/ido321")  
Second, array traversalin PHP, there are three commonly used array traversal methods:1. For Loop1:<?php
   3:     echo"$dwqs [i]<br/>";
2. foreach statement

1: //First way
   3:foreach as $value) {
;
   6:
   8:  <?php
  :     echo $key." = ". $value;
3. While loop

1:<?php
   3:     echo $key.":". $value;

Third, partial array-related functions (using the print_r () function to output array contents)

PHP's array functionality is very powerful and is one of the most commonly used data types. Its processing function also has a strong, efficient characteristics.

1. Key/value operation function of array

1.1 Function Array_values (): Returns the value of all elements in the array. The array name is passed in, the key name is not preserved, and the returned array is re-indexed starting at 0.

1:<?php
"www.ido321.com","program enthusiasts""QQ Group: 259280570");
   4:  print_r (Array_values ($dwqs 2));
"Www.ido321.com", "program enthusiast" = ">" "QQ Group: 259280570"); <= "" Font= "" >
   7:
1.2 Function Array_keys (): Returns the name of the key in the array.

1:<?php
   3:
   5:  print_r (array_kays ($dqws,' ID '));
1.3 function In_array (): Detects if a value exists in the array

1:<?php
   3://output: Presence
   5:     echo"existence";
   7:Else{
   9:  }
2. Number and uniqueness of arrays2.1 function count (): counts the number of elements in an array or the number of attributes in an object1:<?php
   3:  ?>

2.2 Function Array_unique (): Delete duplicate values in array, return array key name remains unchanged

1:<?php
' 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 takes the values 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:<?php
   3:  print_r (array_count_values ($a));
3. Array sorting


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

PHP Array Usage Summary

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.