Home  >  Article  >  Backend Development  >  php array

php array

WBOY
WBOYOriginal
2016-08-08 09:27:041213browse









$arr=array('zz'=>'Zhengzhou','Beijing',9=>'Shanghai','Zhengzhou');
print_r($arr);
$c=array_unique($arr);//Eliminate duplicate element values ​​and arrange by index
print_r($c);
$b=array_values($arr);//Reorder the array
print_r($b);



$arr1=array('Zhengzhou','Hebei','Shanghai','Beijing');
$arr2=array('Kaifeng','Luoyang','Puyang');
$arr3=array_merge($arr1,$arr2);//Merge arrays $arr1,$arr2.
echo '

';
print_r($arr3);





$str="jjjjjj5555455ccccdddd565dfddfd";
$arr=preg_split('/d{1,}/', $str);//regular expression
print_r($arr);



$st="Beijing, Shanghai, Tianjin, Zhengzhou, Guangdong";
$arr=explode(',', $st);
print_r($arr);
$ss=implode("====",$arr);
echo '
';

echo $ss;
foreach ($arr as $v){
echo '

';
echo $v;
}



$arr[][]='ok';
$arr[][]=[10,20,30];
$arr[][]='hello';
$arr[][]=array('hn'=>'Henan',60,'zz'=>'Zhengzhou',array(100,200));
echo '
';
print_r($arr);
echo $arr[1][0][2];


$age=$sage=$stage=50;

echo $age;
echo '

';
echo $sage;
echo $stage;





//two-dimensional array
$arr=array(10,'zz'=>'Zhengzhou',array(20,30,40,50),array(60,70,80));
echo '
';
print_r($arr);
echo $arr[2][0];
echo sizeof($arr,1);
echo count($arr);
foreach($arr as $v){
if(!is_array($v)){
echo $v.'
';
continue;
}
foreach ($v as $vv){
echo $vv.'
';
}
}


$a=[10,20,30,40];
echo in_array(20, $a);//Query whether a value in the array exists, if it exists, return 1
if (array_search(20, $a)===false){
echo 'not found';
}else {
echo 'Found, the location is:'.array_search(20, $a);
}


//It is recommended to specify the number of characters in the sequence
$arr=range('a','z');
$arr=range(1,100,3);
echo '

';
print_r($arr);
substr intercepts the value and in_array determines whether it exists in the array
$arr=array('12','133','135','138');
$aa='1202';
if (in_array(substr($aa,0,2),$arr)) {
echo 'exist'.$aa;
}else {
echo 'does not exist';
}

is_array determines whether it is an array
$arr=20;
if (is_array($arr)){
echo 'is an array';
print_r($arr);
}else {
echo 'not an array';
echo $arr;
}




//Use a loop statement to assign 10 values ​​​​(random integers between 1-100) to the empty array
$arr=array();
for ($i=0;$i<10;$i++){
$arr[]=mt_rand(1,100);
}
echo'After sorting:';
foreach ($arr as $v){

echo $v.' ';
}
echo '


';

// //Implement sorting algorithm bubble sort
for ($m=0;$m for ($n=0;$n if ($arr[$n]>$arr[$n+1]){
$t = $arr[$n];
$arr[$n]=$arr[$n+1];
$arr[$n+1]=$t;
}

}
}

echo'After sorting:';
foreach ($arr as $v){
echo $v.'  ';
}


$arr=array();
$arr[]=10;
$arr[]=20;
$arr[]=30;
$arr[]=40;
$arr[]=50;
echo $arr[2];

echo rand(1,3).','.rand().','.rand(10,100);//Random number
echo mt_rand(1,100).'--'.mt_rand().'--'.mt_rand(1,3);//Generate better random numbers

//Declare an array randomly assigned to integers in the range of 10 numbers (1-100)
$arr=array();
for ($i=0;$i<10;$i++){
$arr[]=mt_rand(1,100);
}
echo '

';
print_r($arr);


sort($arr);//Output array after sorting
print_r($arr);


$arr1=array(10,52,34,40);//Declare the array
$arr2=[10,20,30];
$arr3=array('bj'=>'Beijing','sh'=>'Shanghai');
echo '

';
var_dump($arr1);//Print the array and output type
print_r($arr3);//Print output array

echo $arr3['bj'];//Output Beijing
echo '
';
echo $arr1[2];/output 30, starting from 0

foreach ($arr3 as $k=>$v){
echo $v;//Output value, $k output subscript
}
$arr3[]='Zhengzhou'; //Add elements to the array at the back of the array
$arr3['sz']='Shenzhen';
array_unshift($arr3,'Hangzhou','Qingdao');//Add elements in front of the array
unset($arr3['bj']);//Delete the bj element in the $arr3 array


$bj= array_shift($arr3);//Remove the first element in the array, take it out, assign it to bj and output
echo $bj;

$sz=array_pop($arr3);//Remove the last element in the array and assign it
echo $sz;


foreach ($arr3 as $k=>$v){
echo $k.'=>'.$v;
}

sort($arr1);//Ascending order, from small to large
rsort($arr1);//Descending order, from large to small
print_r($arr1);
echo count($arr1);


$a=array(4,8);
echo count($a);//returns the number of elements in the array 2

$str='Day, one, two, three, four, five, six';
$arr=explode(',',$str); // Divide a string into arrays according to string intervals
// print_r($arr);
$w=date('w');
echo 'Today is: Sunday'.$arr[$w];

$h=date('G');
// if ($h>=8 && $h<=12) {
// echo 'Good morning';
// }else if ($h>=13 && $h<=18){
// echo 'Good afternoon';
// }else if ($h>=19 && $h<=21){
// echo 'Good evening';
// }elseif ($h>=22 && $h<=6){
// echo 'It's late at night, please take a rest';
// }elseif ($h>=6 && $h<=7){
// echo 'Good morning';
// }


// echo date ( 'Y-m-d H:i:s' );
// $d = date ( 'w' );
// if ($d == 0) {
// echo 'Today: Sunday';
// }
// if ($d == 1) {
// echo 'Today: Monday';
// }
// if ($d == 2) {
// echo 'Today: Tuesday';
// }
// if ($d == 3) {
// echo 'Today: Wednesday';
// }
// if ($d == 4) {
// echo 'Today: Thursday';
// }
// if ($d == 5) {
// echo 'Today: Friday';
// }
// if ($d == 6) {
// echo 'Today: Saturday';
// }

?>

The above introduces PHP arrays, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

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