PHP Common Array Operation method Note collation, array operation method _php tutorial

Source: Internet
Author: User
Tags array definition php definition shuffle

How to use the array operation method in PHP


Overview
To access the contents of a variable, you can use its name directly. If the variable is an array, you can use a combination of the variable name and keyword or index to access its contents.
Like other variables, you can change the contents of an array element by using operator =. Array cells can be accessed through the Array[key] syntax.

Basic Operations for arrays
PHP definition array:

<?php   $array = Array ();   

There are two main ways to declare an array in PHP:

1. Declare an array with the array () function,
2. Assign a value directly to the array element.

<?php  //array Arrays  $users = Array (' phone ', ' computer ', ' dos ', ' Linux ');  echo $users;//Only print out data type Array  print_r ($users);//array ([0] = phone [1] = computer [2] = = DOS [3] = Linu x)  $numbers = range (1,5);//Create an array containing the specified range  print_r ($numbers);//array ([0] = 1 [1] = 2 [2] = 3 [3] => ; 4 [4] = 5)  Print_r (true);//1  Var_dump (false);//bool (false)//print_r can simply print strings and numbers, Arrays begin with an array and are represented as key values, Print_r output Boolean values and null results are meaningless, so use var_dump to display all values in the array for  ($i = 0; $i < 5; $i + +) {    echo $users [$i];    Echo '
'; } By count/sizeof the number of cells in the array or the number of attributes in the object for ($i = 0; $i < count ($users); $i + +) { echo $users [$i]; Echo '
'; } You can also iterate through the array through the Foreach loop, which has the advantage of not having to consider key foreach ($users as $value) { echo $value.
';//dot number is a string join symbol }//foreach loop traversal $key = = $value; $key and $value are variable names, you can set the foreach ($users as $key + = $value) { echo $key. '
';//output key }?>

Creating an array of custom keys

<?php  //Create an array of custom keys  $ceo = Array (' apple ' = ' jobs ', ' microsoft ' = ' Nadella ', ' Larry Page ', ' Eric ');  If you do not declare the key of the element, it will start from zero  print_r ($CEO);//array ([Apple] = jobs [Microsoft] = Nadella [0] = Larry Page [1] =& Gt Eric)  echo $ceo [' Apple '];//jobs   //php5.4 usage  $array = [    "foo" + "Bar",    "bar" and "foo",  ];  Print_r ($array);//array ([foo] = bar [bar] = foo)?>  

From php5.4 you can use the short array definition syntax and replace array () with []. A bit similar to the definition of arrays in JavaScript.

Use of each ()

<?php  //Create an array  $ages [' trigkit4 '] = by assigning a value to a set of elements;  echo $ages. '
';//array //Because the index of the associated array is not a number, it cannot be traversed by a for loop, only through the use of the Foreach Loop or list () and each () structure//each/ / Each returns the current key/value pair in the array and moves the array pointer forward one step $users = Array (' TRIGKIT4 ' =>22, ' Mike ' =>20, ' John ' =>30); Print_r (each ($users));//array ([1] = [value] = [0] = = TRIGKIT4 [key] = TRIGKIT4) //equivalent: $a = a Rray ([0]=>trigkit4,[1]=>22,[value]=>22,[key]=>trigkit4); $a = each ($users);//each the first element of the original array and wraps it into a new array and assigns a value to $ A echo $a [0];//trigkit4 //!! Represents the conversion of real-world data to a Boolean value

Each pointer points to the first key-value pair and returns the first array element, gets its key-value pair, and wraps it into a new array

Use of List ()
The list is used to assign the values of the array to some variables, as shown in the following example:

<?php  $a = [' 2 ', ' abc ', ' Def '];  List ($var 1, $var 2) = $a;  echo $var 1. '
';//2 echo $var 2;//abc $a = [' name ' = ' trigkit4 ', ' age ' =>22, ' 0 ' + ' boy ']; List only recognizes key as the index of the number list ($var 1, $var 2) = $a; Echo $var 1;//boy?>

Note: List only knows the index of key as a number

Ordering of array elements
The reverse sort: sort (), Asort (), and Ksort () are both positive and, of course, the corresponding reverse sort.
Implement reverse: Rsort (), Arsort (), and Krsort ().

The Array_unshift () function adds a new element to the array header, and the Array_push () function adds each new element to the end of the array.
Array_shift () deletes the first element of the array header, and its inverse function is array_pop (), deleting and returning an element at the end of the array.
Array_rand () returns one or more keys in the array.

The function shuffle () randomly sorts the elements of an array.
The function Array_reverse () gives a reverse sort of the original array
Use of various types of APIs for arrays
Count () and sizeof () counts the number of array subscripts
Array_count_values () counts the number of subscript values in an array

<?php $numbers = Array (' 100 ', ' 2 '); Sort ($numbers, sort_string);//Sort by string, string only compares first bit size print_r ($numbers);//array ([0] = [1] = 2) $arr = Array (' tr  Igkit4 ', ' banner ', ' 10 ');  Sort ($arr, sort_string);  Print_r ($arr);//array ([0] = [1] = banner [2] = = TRIGKIT4) shuffle ($arr);  Print_r ($arr);//random sort $array = Array (' A ', ' B ', ' C ', ' d ', ' 0 ', ' 1 ');  Array_reverse ($array); Print_r ($array);//reverse order of the original array.  Array ([0] = a [1] = b [2] = = c [3] = d [4] = 0 [5] + 1)//copy of the array $arr 1 = array (' 10 ', 2);  $arr 2 = & $arr 1; $arr 2 [] = 4;  $arr 2 were changed, $arr 1 was still Array ('//array ', 3) Print_r ($arr 2), and the//asort ([0] = [1] = 2 [2] + 4) use $arr 3 =  & $arr 1;//Now ARR1 and ARR3 are the same $arr 3 [] = ' 3 '; Asort ($arr 3);//sorting the array and preserving the original relationship Print_r ($arr 3);//Array ([1] = 2 [2] = 3 [0] = +)//ksort Use $fruits = arr  Ay (' c ' = ' banana ', ' a ' = ' = ' apple ', ' d ' = ' orange ');  Ksort ($fruits); Print_r ($fruits);//array ([A] = Apple [c] = banAna [d] = orange)//unshift use Array_unshift ($array, ' z ');//Add an element Print_r ($array) at the beginning,//array ([0] = Z [1] =&gt ;   A [2] = b [3] = c [4] = + d [5] = 0 [6] = 1)//current (POS) using echo current ($array);//z; Gets the active cell in the current array Next uses echo next ($array);//a the inner pointer in the array moves forward one//reset using echo reset ($array);//z; Pointing the array interior pointer to the first cell//prev using echo NEX  T ($array);//a; Echo prev ($array);//z; Returns a//sizeof using echo sizeof ($array),//7; counts the number of array elements//array_count_values $num = Array (10,20,30,10 , 20,1,0,10);//Count the number of occurrences of an array element Print_r (Array_count_values ($num));//array ([Ten] = 3 [] = 2 [+] = 1 [1] = 1   [0] = 1)?>

Current (): Each array has an internal pointer to his present cell, initially pointing to the first element inserted into the array

For loop traversal

<?php  $value = range (0,120,10);  for ($i =0; $i
 
  

An instance of an array
Use of the Array_pad function

<?php  //array_pad function, array array is selectively appended  $num = Array (1=>10,2=>20,3=>30);  $num = Array_pad ($num, 4,40);  Print_r ($num);//array ([0] = [1] = [2] = [3] = +)  $num = Array_pad ($num, -5,50);//array_pa D (array,size,value)  print_r ($num);//array ([0] = [1] = [2] = [3] = [4] = +)?>

Size: The specified length. Integers are filled to the right, and negative numbers are filled to the left.

Use of unset ()

  use of <?php//unset ()  $num = Array_fill (0,5,rand (1,10)),//rand (Min,max)  print_r ($num);//array ([0] = > 8 [1] = 8 [2] = 8 [3] = 8 [4] = 8)   echo '
'; Unset ($num [3]); Print_r ($num);//array ([0] = 8 [1] = 8 [2] = 8 [4] = 8)?>

Use of Array_fill ()

Use of <?php//array_fill () $num = Range (' A ', ' e '); $arrayFilled = Array_fill ($num);//array_fill (Start,number,value) echo '
';  Print_r ($arrayFilled);? >

Use of Array_combine ()

<? PHP  $number = Array (1,2,3,4,5);  $array = Array ("I", "Am", "A", "PHP", "er");  $newArray = Array_combine ($number, $array);  

Array_splice () Delete array members

<?php  $color = Array ("Red", "green", "blue", "yellow");  Count ($color); Get 4  Array_splice ($color, 1, 1);//delete the second element  Print_r (count ($color));//3  echo $color [2];//yellow  

Array_unique to delete duplicate values in an array

<?php  $color =array ("Red", "green", "blue", "yellow", "blue", "green");  $result = Array_unique ($color);  

Array_flip () Exchange the key values and values of an array

<? PHP  $array = Array ("Red", "Blue", "Red", "Black");  Print_r ($array);  echo "
"; $array = Array_flip ($array);//

Array_search () Search value

 
   <?php  $array = Array ("Red", "Blue", "Red", "Black");  $result =array_search ("Red", $array)//array_search (value,array,strict)  if (($result = = = NULL)) {    echo ' There is no value red ";  } else{    echo "presence value $result";//Presence Value 0   

http://www.bkjia.com/PHPjc/1127879.html www.bkjia.com true http://www.bkjia.com/PHPjc/1127879.html techarticle An array operation method used in PHP, an overview of array manipulation methods to access the contents of a variable, you can use its name directly. If the variable is an array, you can use ...

  • 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.