1 數組函數中sort和asort的區別:sort會把鍵名變為0,1,2之類,不管你之前的數組是什麼;但是asort在排序過程中會以索引值為對象進行排序,但是鍵名不發生改變,並跟隨者索引值移動位置。【arsort是從高到低,asort是從底到高, ksort, krsort是對鍵名排序】。
2 count()和sizeof()用來統計數組的元素個數。
3 in_array() , array_key_exists()
4 快速建立數組 range(0,3) ,這個跟matlab有點類似,php真是多彩啊
5 array_reverse 翻轉 array_sum 求數組之和 array_merge 合并數組 array_flip 對調鍵名和值
6 list($key, $value)=each($balls) reset($..)
7 類似asp中的left,right函數 substr()將兩者合二為一
************
string substr(string string, int start, int [length])
參數string為要操作的字串
參數start為你要截取的字串的開始位置,若start為負數時,則表示從倒數第start開始截取length個字元
選擇性參數length為你要截取的字串長度,若在使用時不指定則預設取到字串結尾。若length為負數時,則表示從start開始向右截取到末尾倒數第length個字元的位置
起初用這個函數時可能感覺到彆扭,不過你要是把PHP substr函數的文法搞懂了,那他的功能比asp中的left和right,有過之無不及,非常好用。下面我們舉例來看他的用法:
1、從第4個字元開始截取到字串末尾,類似asp中的left:
< ?php $str = "www.designline.cn"; echo substr($str,4); ?>
輸出: designline.cn
2、PHP substr函數從右邊截取3個字元,類似asp中的right:
< ?php $str = "www.designline.cn"; echo substr($str,-3); ?>
輸出:.cn
3、PHP substr函數從第4個字元開始截取6個字元:
< ?php $str = "www.designline.cn"; echo substr($str,4,6); ?>
輸出:design
4、有時我們知道一個字串的開始和結尾,中間是不定長的字元,這時除了用PHP substr函數的正則外我們還可以用substr實現(當然要得到中間的字元方法有N種,在此只是舉例substr的應用):
< ?php $str = "< |>www.designline.cn< |>"; echo substr($str,3,-3); ?>
輸出:www.designline.cn
************
把用過的例子附上:
******************************************************************************************************
<?php
$seasons=array("c","x","q","d");
echo "hello array /n";
print_r($seasons);
echo "<br>";
$city=array(
"河北"=>array("北京a1","保定a2"),
"河南b"=>array("b鄭州1","b洛陽2")
);
print_r($city);
//索引數組
$index=array(
1=>"one",
2=>"two",
3=>"three"
);
echo "<br>";
print_r($index);
echo "<br>";
echo $index[1];
//關聯陣列
$language['qiao']="qiao";
$language['zhang']="zhang";
echo "<br>";
echo "qiao is named ".$language['qiao']."<br>";
echo "zhang is named".$language['zhang'];
//basic run
foreach($language as $key=>$value){
echo "<br>$key--$value<br>";
}
//foreach twice
foreach($city as $key=>$value)
{
foreach($value as $value2)
{
echo "<br>$key---$value2<br>";
}
}
for($i=1;$i<3;$i++)
{
$value=$index[$i];
echo "<br>--$i--$value--<br>";
}
//數組添加操作
$ball=array("football","soccer");
$ball[]="basketball";
echo "<br>";
print_r($ball);
//刪除
unset($ball[2]);
function myprint($a)
{
echo "<br>";
print_r($a);
}
myprint($ball);
//update
$ball[2]="basketba";
myprint($ball);
$ball[2]="basketball";
myprint($ball);
//sort
$color=array("yellow","red","green","grey");
sort($color);
myprint($color);
$number=array(2,34,4,342,32);
myprint($number);
sort($number,SORT_NUMERIC);
myprint($number);
foreach($number as $value)
{
echo $value." ";
}
//在指定鍵名的情況下,sort會導致紊亂
$cl=array("yel"=>"yellow","re"=>"red","ge"=>"green","bl"=>"blue");
$cl2=$cl;
sort($cl);
foreach($cl as $key=>$value)
{
echo "<br>$key--$value--<br>";
echo "<br>**************<br>";
}
asort($cl2);
function printA($a)
{
foreach($a as $key=>$value)
{
echo "<br>*****************<br>";
echo "$key--->$value<br>";
// echo "<br>****************<br>";
}
}
myprint($cl2);
printA($cl2);
//try arsort
arsort($cl2);
echo "<br> &&&&&&&&&&&& arsort <br>";
printA($cl2);
// ksort , krsort
$keys=array("b"=>"A","a"=>"B","c"=>"C");
ksort($keys);
printA($keys);
krsort($keys);
printA($keys);
// random sort
shuffle($keys);
printA($keys);
$have=array("qiao","yong");
if(in_array("qiao",$have,true))
{
echo "<br>is in<br>";
}
$have['wang']="meinv";
if(array_key_exists("wang",$have))
{
echo "<br> key is in <br>";
}
$num=count($have);
echo $num;
$quick=range(23,89,10);
myprint($quick);
$quickA=range('A','D');
myPrint($quickA);
// reverse
myPrint(array_reverse($quick));
echo array_sum($quick);
//myPrint(array_merge($quick,$quickA));
$sum2= array_sum(array_merge($quick,$quickA));
echo $sum2;
myprint(array_flip($quick));
// each() list() reset()
// 這個很有意思
$final=array(1=>"eat",2=>"listen",3=>"look");
while(list($key,$value)=each($final))
{
echo $key."....".$value;
}
reset($final);
list($nowkey,$nowvalue)=each($final);
echo $nowkey.".....".$nowvalue;
?>
*******************************************************************