PHP array, php array length _php tutorial

Source: Internet
Author: User
Tags echo date

PHP array, php array length










$arr =array (' zz ' = ' zhengzhou ', ' Beijing ',9=> ' Shanghai ', ' Zhengzhou ');
Print_r ($arr);
$c =array_unique ($arr);//Eliminate duplicate element values and make index arrangements
Print_r ($c);
$b =array_values ($arr);//Reorder Arrays
Print_r ($b);



$arr 1=array (' Zhengzhou ', ' Hebei ', ' Shanghai ', ' Beijing ');
$arr 2=array (' Kaifeng ', ' Luoyang ', ' Puyang ');
$arr 3=array_merge ($arr 1, $arr 2);//merge array $arr1, $arr 2.
Echo '

';
Print_r ($arr 3);





$str = "JJJJJJ5555455CCCCDDDD565DFDDFD";
$arr =preg_split ('/\d{1,}/', $str);//Regular expressions
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 ', ' a ', ' 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 arrays
$arr =array (' 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 ($a);//query whether a value exists in the array returns 1 if present
if (Array_search ($a) ===false) {
echo ' not found ';
}else {
Echo ' found, position is: '. Array_search ($a);
}


Recommended number of characters for the specified sequence
$arr =range (' A ', ' Z ');
$arr =range (1,100,3);
Echo '

';
Print_r ($arr);
Substr intercept the value, In_array determine if there is an array
$arr =array (' 12 ', ' 133 ', ' 135 ', ' 138 ');
$aa = ' 1202 ';
if (In_array (substr ($AA, 0,2), $arr)) {
echo ' presence '. $aa;
}else {
Echo ' does not exist ';
}

Is_array Judging is not an array
$arr = 20;
if (Is_array ($arr)) {
Echo ' is an array ';
Print_r ($arr);
}else {
Echo ' is not an array ';
Echo $arr;
}




Assigning 10 values (random integers between 1-100) to an empty array using a circular statement
$arr =array ();
for ($i =0; $i <10; $i + +) {
$arr []=mt_rand (1,100);
}
Echo ' sort after: ';
foreach ($arr as $v) {

echo $v. ";
}
Echo ';

Implement sort 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 ' sort after: ';
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

Declares 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);


$arr 1=array (10,52,34,40);//Declaration array
$arr 2=[10,20,30];
$arr 3=array (' bj ' = ' Beijing ', ' sh ' = ' Shanghai ');
Echo '

';
Var_dump ($arr 1);//print array and output type
Print_r ($arr 3);//Print output array

echo $arr 3[' BJ '];//output Beijing
Echo '
';
echo $arr 1[2];/output 30, starting from 0

foreach ($arr 3 as $k = = $v) {
echo $v;//output value, $k output subscript
}
$arr 3[]= ' Zhengzhou '; Adds an element to an array after the array
$arr 3[' sz ']= ' Shenzhen ';
Array_unshift ($arr 3, ' Hangzhou ', ' Qingdao ');//Add elements to the front of the array
unset ($arr 3[' BJ ');//delete the BJ element in the $ARR3 array


$BJ = Array_shift ($arr 3);//The first element in the array is removed to assign to BJ and output
Echo $BJ;

$sz =array_pop ($arr 3);//Move the last element in the array and assign a value
Echo $sz;


foreach ($arr 3 as $k = = $v) {
echo $k. ' = '. $v;
}

Sort ($arr 1);//ascending, from small to large
Rsort ($arr 1);//descending, from large to small
Print_r ($arr 1);
echo Count ($arr 1);


$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 by string interval
Print_r ($arr);
$w =date (' W ');
Echo ' Today is: Week '. $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 ' Late at night, please pay attention to 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 ';
// }

?>

http://www.bkjia.com/PHPjc/975480.html www.bkjia.com true http://www.bkjia.com/PHPjc/975480.html techarticle PHP Array, php array length? PHP $arr =array (' zz ' = ' zhengzhou ', ' Beijing ', 9 = ' Shanghai ', ' Zhengzhou '); Print_r ($arr); $c =array_unique ($arr);//Eliminate duplicate element values, and index permutation pri ...

  • Related Article

    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.