Table of Contents
php数组,php数组长度
Home php教程 php手册 php数组,php数组长度

php数组,php数组长度

Jun 13, 2016 am 09:09 AM
php

php数组,php数组长度









$arr=array('zz'=>'郑州','北京',9=>'上海','郑州');
 print_r($arr);
 $c=array_unique($arr);//消除重复的元素值,并进行索引排列
 print_r($c);
 $b=array_values($arr);//重新排序数组
 print_r($b);



 $arr1=array('郑州','河北','上海','北京');
 $arr2=array('开封','洛阳','濮阳');
 $arr3=array_merge($arr1,$arr2);//合并数组$arr1,$arr2.
 echo '

';<br> print_r($arr3);<br> <br> <br> <br> <br> <br> $str="jjjjjj5555455ccccdddd565dfddfd";<br> $arr=preg_split('/\d{1,}/', $str);//正则表达式<br> print_r($arr);<br> <br> <br> <br> $st="北京,上海,天津,郑州,广东";<br> $arr=explode(',', $st);<br> print_r($arr);<br> $ss=implode("====",$arr);<br> echo '<pre class="brush:php;toolbar:false">';
<p> echo $ss;<br> foreach ($arr as $v){<br> echo '</p><pre class="brush:php;toolbar:false">';<br> echo $v;<br> }<br> <br> <br> <br> $arr[][]='ok';<br> $arr[][]=[10,20,30];<br> $arr[][]='hello';<br> $arr[][]=array('hn'=>'河南',60,'zz'=>'郑州',array(100,200));<br> echo '<pre class="brush:php;toolbar:false">';<br> print_r($arr);<br> echo $arr[1][0][2];<br> <br> <br> $age=$sage=$stage=50;
<p> echo $age;<br> echo '</p><pre class="brush:php;toolbar:false">';<br> echo $sage;<br> echo $stage;<br> <br> <br> <br> <br> <br> //二维数组<br> $arr=array(10,'zz'=>'郑州',array(20,30,40,50),array(60,70,80));<br> echo '<pre class="brush:php;toolbar:false">';<br> print_r($arr);<br> echo $arr[2][0];<br> echo sizeof($arr,1);<br> echo  count($arr);<br> foreach($arr as $v){<br> if(!is_array($v)){<br> echo $v.'<br>';<br> continue;<br> }<br> foreach ($v as $vv){<br> echo $vv.'<br>';<br> }<br> } <br> 
<p> <br> $a=[10,20,30,40];<br> echo in_array(20, $a);//查询数组中某个值是否存在如果存在则返回1<br> if (array_search(20, $a)===false){<br> echo '没有找到';<br> }else {<br> echo '找到了,位置是:'.array_search(20, $a);<br> }<br> <br> <br> //建议指定序列的字符数<br> $arr=range('a','z');<br> $arr=range(1,100,3);<br> echo '</p><pre class="brush:php;toolbar:false">';<br> print_r($arr);<br>substr截取数值,in_array 判断是否存在数组中<br> $arr=array('12','133','135','138');<br> $aa='1202';<br> if (in_array(substr($aa,0,2),$arr)) {<br> echo '存在'.$aa;<br> }else {<br> echo '不存在';<br> }
<p> </p>
<p> </p>
<p>is_array判断是不是数组<br> $arr=20;<br> if (is_array($arr)){<br> echo '是数组';<br> print_r($arr);<br> }else {<br> echo '不是数组';<br> echo $arr;<br> }<br> <br> <br> <br> <br> //利用循环语句给空数组赋10个值(1-100之间的随机整数)<br> $arr=array();<br> for ($i=0;$i $arr[]=mt_rand(1,100);<br> }<br> echo'排序后:';<br> foreach ($arr as $v){</p>
<p> echo $v.'  ';<br> }<br> echo '</p><hr>';
<p>// //实现排序算法冒泡排序<br> for ($m=0;$m<count></count> for ($n=0;$n<count></count> if ($arr[$n]>$arr[$n+1]){<br> $t = $arr[$n];<br> $arr[$n]=$arr[$n+1];<br> $arr[$n+1]=$t;<br> }<br>		<br> }<br> }</p>
<p> echo'排序后:';<br> foreach ($arr as $v){<br> echo $v.' &nbsp';<br> }</p>
<p> <br> $arr=array();<br> $arr[]=10;<br> $arr[]=20;<br> $arr[]=30;<br> $arr[]=40;<br> $arr[]=50;<br> echo $arr[2];</p>
<p> </p>
<p> </p>
<p> </p>
<p> echo rand(1,3).','.rand().','.rand(10,100);//随机数<br> echo mt_rand(1,100).'--'.mt_rand().'--'.mt_rand(1,3);//生成更好的随机数</p>
<p> </p>
<p> </p>
<p>//声明一个数组随机赋予10个数字(1-100)范围内的整数<br> $arr=array();<br> for ($i=0;$i $arr[]=mt_rand(1,100);<br> }<br> echo '</p><pre class="brush:php;toolbar:false">';<br> print_r($arr);
<p><br> sort($arr);//排序后输出数组<br> print_r($arr);</p>
<p> </p>
<p><br> $arr1=array(10,52,34,40);//声明数组<br> $arr2=[10,20,30];<br> $arr3=array('bj'=>'北京','sh'=>'上海');<br> echo '</p><pre class="brush:php;toolbar:false">';<br> var_dump($arr1);//打印数组并且输出类型<br> print_r($arr3);//打印输出数组
<p> echo $arr3['bj'];//输出北京<br> echo '<br>';<br> echo $arr1[2];/输出30,从0开始</p>
<p> foreach ($arr3 as $k=>$v){<br> echo $v;//输出值,$k输出下标<br> }<br> $arr3[]='郑州';  //给数组增加元素在数组的后面<br> $arr3['sz']='深圳';<br> array_unshift($arr3,'杭州','青岛');//在数组的前面增加元素<br> unset($arr3['bj']);//删除$arr3数组中的bj元素</p>
<p><br> $bj= array_shift($arr3);//移除数组中的第一个元素,取出来赋给bj并输出<br> echo $bj;</p>
<p> $sz=array_pop($arr3);//移除数组中的最后一个元素,并赋值<br> echo $sz;</p>
<p><br> foreach ($arr3 as $k=>$v){<br> echo $k.'=>'.$v;<br> }</p>
<p> </p>
<p>sort($arr1);//升序,从小到大<br> rsort($arr1);//降序,从大到小<br> print_r($arr1);<br> echo count($arr1);</p>
<p> </p>
<p> </p>
<p> </p>
<p><br> $a=array(4,8);<br> echo count($a);//返回数组的元素个数2</p>
<p> </p>
<p> $str='日,一,二,三,四,五,六';<br> $arr=explode(',',$str);//将一个字符串按照字符串间隔分成数组<br> // print_r($arr);<br> $w=date('w');<br> echo '今天是:星期'.$arr[$w];</p>
<p> </p>
<p> $h=date('G');<br>// if ($h>=8 && $h// 	echo '上午好';<br>// }else if ($h>=13 && $h// 	echo'下午好';<br>// }else if ($h>=19 && $h// 	echo '晚上好';<br>// }elseif ($h>=22 && $h// 	echo '夜深了,请注意休息';<br>// }elseif ($h>=6 && $h// 	echo '早上好';<br>// }</p>
<p> </p>
<p><br>// echo date ( 'Y-m-d H:i:s' );<br>// $d = date ( 'w' );<br>// if ($d == 0) {<br>// 	echo '今日:星期日';<br>// }<br>// if ($d == 1) {<br>// 	echo '今日:星期一';<br>// }<br>// if ($d == 2) {<br>// 	echo '今日:星期二';<br>// }<br>// if ($d == 3) {<br>// 	echo '今日:星期三';<br>// }<br>// if ($d == 4) {<br>// 	echo '今日:星期四';<br>// }<br>// if ($d == 5) {<br>// 	echo '今日:星期五';<br>// }<br>// if ($d == 6) {<br>// 	echo '今日:星期六';<br>// }</p>
<p>?><br>          </p>
Copy after login
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles