Home >php教程 >php手册 >php基础学习笔记(4),php基础学习笔记

php基础学习笔记(4),php基础学习笔记

WBOY
WBOYOriginal
2016-06-13 09:12:33944browse

php基础学习笔记(4),php基础学习笔记

数组介绍

概念: 就是将若干个数据以一定的顺序放在一起的一个集合体,整体上就称之为“数组”。数组就是一列数据的有序排列的集合。

定义形式:

<span>var</span>  arr1 = <span>new</span> Array(<span>1</span>,  <span>5</span>,  <span>8</span>,  <span>7</span>,  <span>2</span>,  <span>10</span>);    <span>//</span><span>定义了一个数组,其中具有6个数据</span>
    <span>var</span>  arr2 = <span>new</span> Array();        <span>//</span><span>只是单纯地定义了一个数组(名),但没有给值(数据),即现在是空的</span>
    <span>var</span>  arr3 = [<span>1</span>,  <span>5</span>,  <span>8</span>,  <span>7</span>,  <span>2</span>,  <span>10</span>];    <span>//</span><span>同arr1,只是一种简写的定义法。</span>
    <span>var</span>  arr4 = [ ];        <span>//</span><span>同arr2,也是一个空数组。</span>

数组的使用:所谓使用其实是指对数组的每一项的使用。

         取值:

    <span>var</span>  v1 = arr1[<span>0</span>];    <span>//</span><span>取得数组arr1中的第一项,0叫做下标</span>
        <span>var</span>  v2 = arr3[<span>3</span>] + <span>10</span>;    <span>//</span><span>取得数组arr3中的第4项,4叫做下标</span>
        &mdash;&mdash;所谓下标,其实就是数组的每一个数据的&ldquo;顺序号&rdquo;&mdash;&mdash;从0开始编号,是连续的整数。

         赋值:

                   arr1[0] = 10;     //给数组arr1的第一项赋值为10,此时其实也就是相当于修改了其值,也可以说类似给一个变量重新赋值。

arr2[<span>0</span>]  = <span>22</span><span>;
        arr2[</span><span>1</span>]  = <span>33.3</span><span>;
        arr2[</span><span>2</span>]  = &ldquo;<span>444</span><span>&rdquo;;
        arr2[</span><span>3</span>]  =<span> &ldquo;abc&rdquo;;
        arr2[</span><span>4</span>]  = <span>true</span>;

//此时arr2这个数组相当于这样: [ 22, 33.3, “444”, “abc”, true ]

数组的“视觉形象”(以arr3为例):

下标值:    <span>0</span>    <span>1</span>    <span>2</span>    <span>3</span>    <span>4</span>    <span>5</span><span>
数据值:    </span><span>1</span>    <span>5</span>    <span>8</span>    <span>7</span>    <span>2</span>    <span>10</span>

取得一个数组的长度——就是其中的数据的个数的语法为:

         var  v1 = 数组名.length;

         特别注意:数组的最大下标是数组的长度减1。

数组遍历的通常模式:

var len = 数组名.length;

for(var i = 0;  i

{

         //这里就是对数组的每一项的处理,每一项的写法是:  数组名[i]

}

数组遍历的另一种形式——for in循环语句。

for( var  v1   in  数组名arr1 )

{

         //这里就是循环体,是专门针对数组arr1的遍历循环,其中v1的值就是表示数组的每一项的下标值。

         //v1只是一个“临时变量”,代表每一项下标,依次会从0变化到数组的最大下标。

}

 

“二维”数组:

<span>var</span>  v1 = [<span>2</span>, <span>5</span>, <span>1</span>, <span>5</span><span>];
</span><span>var</span>  v2 = [<span>5</span>, <span>1</span>, <span>6</span>, <span>8</span><span>];
</span><span>var</span>  v3 = [<span>8</span>, <span>0</span>, <span>9</span>, <span>7</span><span>];
</span><span>var</span>  v4 =<span> [v1,  v2,  v3];
</span><span>var</span>  v5 =<span> [  
[</span><span>2</span>, <span>5</span>, <span>1</span>, <span>5</span><span>], 
[</span><span>5</span>, <span>1</span>, <span>6</span>, <span>8</span><span>], 
[</span><span>8</span>, <span>0</span>, <span>9</span>, <span>7</span><span>]
]; </span>

——v4和v5其实没有任何区别,这两个都可以称为“二维数组“。

 

“二维”数组元素的操作:

         取值:

                   var s1 = v5[0][1];      //5    //相当于取到v5这个数组中第一项(这还是一个数组)的第2项。

                   var s2 = v5[2][3] + 100;    //107

         赋值:

                   v5[0][1] = 200;

                   v5[2][3] = 300;

数组对象的常用方法:

什么叫方法:方法其实就是函数!——只是如果一个函数“隶属于”某个“对象”,则称这个函数为该对象的方法。

<span>function maibao(){
        document.write(&ldquo;啦啦啦,我是卖报的小行家,卖报啦卖报啦。&rdquo;);
}
</span><span>var</span> myDreamGirl =<span> { 
name: &ldquo;小花&rdquo;, 
age:</span><span>18</span><span>, 
edu:&rdquo;大学&rdquo;,  
sex:&rdquo;女&rdquo;,
nengli1: function (){ document.write(&ldquo;洗衣!&rdquo;); } ,
nengli2: function (){ document.write(&ldquo;做饭!&rdquo;); } ,
nengli3: maibao
};
</span><span>var</span>  v1 = [<span>2</span>, <span>5</span>, <span>1</span>, <span>5</span><span>];
</span><span>var</span>  v2 = [<span>5</span>, <span>1</span>, <span>6</span>, <span>8</span>];

从严格的角度来说,数组也是一种对象——甚至字符串也是对象。

v1作为对象,就有属性和方法:

         属性:

                   某数组.length: 表示该数组对象的长度

         方法:

                   某数组.concat(其他数组):将两个数组连接起来成为一个新的“更长”的数组。
                   var s1 = v1.concat( v2 );  //此时s1是这样一个数组: [2, 5, 1, 5, 5, 1, 6, 8];

                  

                   某数组.join(“字符串”):将数组中的所有项以指定的字符“串接起来”成为“长的”一个字符串。

                   var s2 = v1.join(“//”);       //结果s2为字符串 “2//5//1//5”

                  

                   某数组.pop();           //将该数组的最后一项“移除”(删除),并返回该项数据,即该数组少了一项

                   var  s3 = v1.pop();           //结果v1只剩这个:[2,5,1]; s3的值是5

 

                   某数组.push(新数据项d1);  //将新的数据d1添加到该数组的最后位置,即数组多了一项。

                   var  s4 = v1.push( 55 );          //v1此时为:[2,5,1, 55],  s4的值为新数组的长度,即4

 

                   某数组.shift();                   //将该数组的第一项“移除”(删除),并返回该项数据,即该数组少了一项

                   var  s5 = v1.shift();                   //结果v1只剩这个:[5, 1,55]; s5的值是2

        

                   某数组.unshift(新数据项d1);  //将新的数据d1添加到该数组的最前位置,即数组多了一项。

                   var  v6 = v1.unshift( 66 );       //v1此时为:[66, 5, 1, 55],  s6的值为新数组的长度,即4

 

javascript语言是一门基于对象的语言。

字符串对象:

<span>var</span> str1 = <span>new</span> String(&ldquo;abcdefgabc&rdquo;);    <span>//</span><span>这是一个&ldquo;字符串对象&rdquo;</span>
    <span>var</span> str2 = &ldquo;abcdefgabc&rdquo;;                <span>//</span><span>这个字符串跟前面str1几乎没有区别</span>
<span>字符串对象的属性:
        .length&mdash;&mdash;获得一个字符串的长度(也就是字符个数)
字符串对象的方法:
</span><span>1</span>.    str1.charAt( n );    &mdash;&mdash;获得字符串str1中位置为n的那个字符(字符的位置也是从0开始算起)<span>var</span> s1 = str1.charAt( <span>3</span> );        <span>//</span><span>s1的结果是:&rdquo;d&rdquo;</span>
<span>2</span><span>.    str1.toUpperCase();    &mdash;&mdash;获取str1全部转换为大写的结果
</span><span>var</span> s2 = str1.toUpperCase();    <span>//</span><span>s2的结果是:&rdquo;ABCDEFGABC&rdquo;</span>
<span>3</span><span>.    str1.toLowerCase();    &mdash;&mdash;获取str1全部转换为小写的结果
</span><span>var</span> s3 = str1.toLowerCase();    <span>//</span><span>s3的结果是:&rdquo;abcdefgabc&rdquo;</span>
<span>4</span><span>.    str1.replace(&ldquo;字符1&rdquo;, &ldquo;字符2&rdquo;);    &mdash;&mdash;将str1中的&ldquo;字符1&rdquo;替换为&ldquo;字符2&rdquo;
</span><span>var</span> s4 = str1.replace(&ldquo;cd&rdquo;, &ldquo;<span>999</span>&rdquo;);    <span>//</span><span>s4的结果是:&rdquo;ab999efgabc&rdquo;</span>
<span>5</span>.    str1.indexOf(&ldquo;字符1&rdquo;); &mdash;&mdash;获得&ldquo;字符1&rdquo;在str1中第一次出现的位置,如果没有出现,结果是-<span>1</span>
<span>var</span> s5 = str1.indexOf(&ldquo;ab&rdquo;);        <span>//</span><span>s5的结果是0</span>
<span>6</span>.    str1.lastIndexOf(&ldquo;&ldquo;字符1&rdquo;); &mdash;&mdash;获得&ldquo;字符1&rdquo;在str1中最后一次出现的位置,如果没有出现,结果是-<span>1</span>
<span>var</span> s6 = str1.lastIndexOf(&ldquo;ab&rdquo;);        <span>//</span><span>s6的结果是7</span>
<span>7</span><span>.    str1.substr(n, m )    &mdash;&mdash;取得str1中从位置n开始的m个字符,m可以省略,则表示从位置n一直取到字符串的最后&mdash;&mdash;注意,这种&ldquo;取&rdquo;并不影响str1这个原始字符
</span><span>var</span> s7 = str1.substr(<span>2</span>, <span>4</span>);    <span>//</span><span>s7为:&rdquo;cdef&rdquo;</span>
<span>8</span><span>.    str1.substring( n, m )&mdash;&mdash;取得str1中从位置n到位置m的前一个字符。
</span><span>var</span> s8 = str1.substring(<span>2</span>, <span>4</span>);    <span>//</span><span>s8为:&rdquo;cd&rdquo;</span>
<span>9</span><span>.    str1.split(&ldquo;字符1&rdquo;)    &mdash;&mdash;将str1以指定的&ldquo;字符1&rdquo;为分界,分割成一个数组,结果是一个数组
</span><span>var</span> s9 = str1.split(&ldquo;b&rdquo;);    <span>//</span><span>s9的结果是一个数组:[&ldquo;a&rdquo;, &ldquo;cdefga&rdquo;, &ldquo;c&rdquo;]</span>

Math对象

Math对象是一个系统内部定义的对象,我们无需去“新建一个Math对象”——跟string对象和array对象不同。即Math对象是直接使用的。

我们学习Math对象,无非是学习一些常见的数学处理函数——这里当就叫做方法了:

属性:

         Math.PI——代表圆周率这个“常数”

方法:

<span>1</span><span>.    Math.max(数值1,数值2,&hellip;..) &mdash;&mdash;求得若干个数值中的最大值。
</span><span>2</span><span>.    Math.min(数值1,数值2,&hellip;..)     &mdash;&mdash;求得若干个数值中的最小值。
</span><span>3</span><span>.    Math.abs( 数值1)     &mdash;&mdash;求得数值1的绝对值
</span><span>4</span><span>.    Math.pow( x,y)        &mdash;&mdash;求得数值x的y次方,也就是&ldquo;幂运算&rdquo;
</span><span>5</span><span>.    Math.sqrt( x )            &mdash;&mdash;求得x的开方
</span><span>6</span><span>.    Math.round( x )         &mdash;&mdash;求得x的四舍五入的结果值;
</span><span>7</span><span>.    Math.floor( x )            &mdash;&mdash;求得x的向下取整的结果,即找到不大于x的一个最大的整数。
a)    Math.floor( </span><span>3.1</span> )  <span>3</span><span>
b)    Math.floor( </span><span>3.8</span> )  <span>3</span><span>
c)    Math.floor( </span><span>3</span> )  <span>3</span><span>
d)    Math.floor( </span>-<span>3.1</span> )  -<span>4</span><span>
e)    Math.floor( </span>-<span>3.8</span> )  -<span>4</span>
<span>8</span><span>.    Math.ceil( x )             &mdash;&mdash;求得x的向上取整的结果,即找到不小于x的一个最小的整数
a)    Math.floor( </span><span>3.1</span> )  <span>4</span><span>
b)    Math.floor( </span><span>3.8</span> )  <span>4</span><span>
c)    Math.floor( </span><span>3</span> )  <span>3</span><span>
d)    Math.floor( </span>-<span>3.1</span> )  -<span>3</span><span>
e)    Math.floor( </span>-<span>3.8</span> )  -<span>3</span>
<span>9</span>.    Math.random()        &mdash;&mdash;获得一个0~<span>1范围中的随机&ldquo;小数&rdquo;,含0,但不含1


</span><span>//</span><span>获得两个整数(n1,n2)之间的随机整数的通用做法:</span>
    <span>var</span> v1 =<span> Math.random()
    v1 </span>= v1 * (n2-n1+<span>1</span><span>);
    v1 </span>= Math.floor( v1 ) + n1;    <span>//</span><span>此时v1就是结果
</span><span>//</span><span>将上述3步写出一步就是(通用公式):</span>
    <span>var</span> v1 = Math.floor( Math.random() * (n2-n1+<span>1</span>) ) + n1

 

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