PHP commonly used array function


We have a lot of elements that manipulate arrays, let's talk about some of them in this section. In 6.3 we will summarize more arrays of commonly used functions.

The following are mainly the functions of moving array pointers and pressing the elements of the popup array.

function Features


Array_shiftPop the first element in the array
Array_unshiftPush the element at the beginning of the array
Array_pushPush the element to the end of the array
Array_popPops the last element at the end of the array
CurrentRead the value of the current position of the pointer
Key
Read the key of the current position of the pointer

Next Pointer down
Prev Move up
Reset Pointer to the beginning
End Pointer to the end

Array_shift


Mixed array_shift ( array & $array )

Function: pops the first element in the array

<?php
$mingren = array("Deng Chao", "Huang Xiaoming", "Ning Zetao", "Zhong Hanliang");
$dc = array_shift($mingren);
Echo $dc .'
';
Print_r($mingren);
?>

Results of the:
 
in conclusion:

    1. Pop the first array element, changing the result of the original array

    2. The value of the popup is assigned to $dc

Array_unshift


Int array_unshift ( array & $ array, mixed $ value 1 [, mixed $... ] )

Function: Push one or more elements to the beginning of the index group, returning the total number.

<?php
$Name= array("Akshay", "Dhiman");
$dc = array_unshift($mingren, "Ning Zetao", "Zhong Hanliang");
Echo $dc .'
';
Print_r($Name);
?>


Array_pop


Mixed array_pop ( array & $array )

Function: pops up an element at the end of the array

<?php
$mingren = array("Deng Chao", "Huang Xiaoming", "Ning Zetao", "Zhong Hanliang");
$dc = array_pop($mingren);
Echo $dc .'
';
Print_r($mingren);
?>

 Array_push


Int array_push ( array &$array , mixed $value1 [, mixed $... ] )

Function: Push one or more elements to the end of the index group, returning the total number.

<?php
$name= array("Akshay", "Dhiman");
$dc = array_push($mingren, "Ning Zetao", "Zhong Hanliang");
Echo $dc .'
';
Print_r($name);
?>
 
Current,key,prev,next,reset function demonstration

These function functions have been said very clearly. We use the code to demonstrate:

<?php
$t=array(
   'we',
   'yy' => 'forever',
   'dbg' => 'Need to keep trying to make progress',
   'djn' => 'can create the future'
   );

/ / Read the value of the array
Echo current($t).'
';
/ / Read the keys of the array
Echo key($t).'
';
// Move backwards
Next($t);
//Reread values ​​and keys
Echo current($t).'
';
Echo key($t).'
';

// Move backwards
Next($t);
Echo current($t).'
';
Echo key($t).'
';

// Move forward
Prev($t);
Echo current($t).'
';
Echo key($t).'
';

/ / Move to the end
End($t);
Echo current($t).'
';
Echo key($t).'
';
/ / Move to the beginning
Reset($t);
Echo current($t).'
';
Echo key($t).'
';

// destroy the array
Unset($t);
Var_dump($t);
?>

Last Updated: 2018-07-06 12:32:44 By akshay

SHARE THIS

share_facebook share_twitter share_google_plus

Please Comment Here :