Home >headlines >PHP commonly used array functions

PHP commonly used array functions

无忌哥哥
无忌哥哥Original
2018-06-28 10:12:582028browse

* Functions should mainly understand the following points?

* 1. Function: What can it do?

* 2. Scenario: Where is it used?

* 3. Parameters: required and optional

* 4. Return value: type and quantity

* 1. Key and value operations (6)

* 1. in_array(value, array): Whether val is in array, return Boolean value

* 2. array_key_exists(key, array): Whether key is in array, return Boolean value

* 3. array_values(array): Returns the value part of array in index mode

* 4. array_keys(array[,value]): Returns the key part of array in index mode, and can also return the key of the specified value

* 5. array_search(value, array): Return the key of the specified value in string format

* 6. array_filp(array): Key value swap

* 2. Inside the array Pointer (cruise) operations (8)

* 1. key(array): returns the key of the current element

* 2. current(array): the value of the current element, pos() It is a function with the same name

* 3. next(array): The pointer moves down, pointing to the next element, and returns the current value

* 4. reset(array): The pointer is reset, pointing to the first element element, and return its value

* 5. end(array): The pointer moves to the last element

* 6. prev(array): The pointer moves forward one bit, and returns The value of the current element

* 7. each(array): Returns the index and associated description of the key value of the current element, and automatically moves the pointer down

* 8. list($a ,$b,...): Assign the values ​​in the index array to a set of variables

echo &#39;<pre class="brush:php;toolbar:false">&#39;;
$user = [&#39;id&#39;=>5,&#39;name&#39;=>&#39;peter&#39;,&#39;gender&#39;=>&#39;male&#39;,&#39;age&#39;=>30];
print_r($user); //查看数组
echo &#39;<hr color="red">&#39;;

//1. Commonly used operation functions for keys and values ​​in arrays

// 1. in_array(value, array): Whether val is in the array, return a Boolean value

echo in_array(&#39;Peter Zhu&#39;,$user) ? &#39;存在<br>&#39; : &#39;不存在<br>&#39;;

//2. array_key_exists(key, array): Whether the key is in the array, return a Boolean value

echo array_key_exists(&#39;name&#39;,$user) ? &#39;存在<br>&#39; : &#39;不存在<br>&#39;;

//3.array_values(array): Return the value part of array in index mode

print_r(array_values($user));

//4.array_keys(array[,value]): Return the key part of array in index mode

print_r(array_keys($user));

//4-1.array_keys(array[,value]): Returns the key part of the array in index mode, and can also return the key of the specified value

print_r(array_keys($user,&#39;male&#39;));

//5. array_search(value, array): Return the key of the specified value in string mode

print_r(array_search(&#39;peter&#39;, $user));

//6. array_filp(array): key value swap

print_r(array_flip($user));

// 2. Array internal pointer (cruise) operation

//count(array)The number of elements in the current array

echo count($user),&#39;<br>&#39;;

//The current pointer points to the first element

//1.key(array): Returns the key of the current element

echo key($user),&#39;<br>&#39;;

//2.current(array): The value of the current element, pos() is the function of the same name

 echo  current($user), &#39;<br>&#39;;

//3.next(array): The pointer moves down, pointing to the next Element

next($user);

//View the key value of the current element

 echo key($user),&#39;<br>&#39;;
 echo  current($user), &#39;<br>&#39;;

//Next(array) can also return in addition to moving the pointer down The value of the current element

var_dump(next($user)); 
 echo key($user),&#39;<br>&#39;;
 echo  current($user), &#39;<br>&#39;;

//Continue to traverse downwards and find that next(array) returns false, indicating that the traversal is over

var_dump(next($user));

//There is no data at the end, there will be no more output

 echo key($user),&#39;<br>&#39;;
 echo  current($user), &#39;<br>&#39;;

//4.reset(array): The pointer is reset, points to the first element, and returns its value

reset ($user);

//In addition to reset, the value of the first element can also be returned

// var_dump(reset($user));

echo key($user),&#39;<br>&#39;;
echo  current($user), '
';

//5. end(array): The pointer moves to the last element

end($user);

// var_dump(end($user)); //Can also return the current The value of the element

echo key($user),&#39;<br>&#39;;
echo  current($user), '
';

//6. prev(array): The pointer moves forward one bit and returns the value of the current element

prev($user);
echo key($user),&#39;<br>&#39;;
echo  current($user), '
';

//7. each(array): Returns the current The index of the element's key value and the associated description of the array, and the pointer is automatically moved down

* Returns an array of four elements:

* 2 index elements, [0] is the key, [ 1] is the value

* 2 associated elements, [key] is the key, [value] is the value

@print_r(each($user));

/ /Note: Due to execution efficiency issues, this function has been abandoned in php7

//8. list($a,$b,...): Assign the value in the index array to a group Variable

reset($user);  //复位指针

* Operation performed:

* 1.each($user): Get the index part of the current element

* 2.list($key, $value ): The value of [0] is assigned to the variable $key, the value of [1] is assigned to the variable $value

* 3. The array $user pointer automatically moves down

list($key, $value) = each($user);
echo $key,&#39;=>&#39;.$value,&#39;<br>&#39;;

//Repeated call

list($key, $value) = each($user);
echo $key,&#39;=>&#39;.$value,&#39;<br>&#39;;

//Very suitable for loop implementation

echo &#39;<hr color="blue">&#39;;
reset($user);
while(list($key, $value) = each($user)) {
echo $key,&#39;=>&#39;.$value,&#39;<br>&#39;;
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to use php while loopNext article:How to use php while loop