标签:php
PHP数组用array来表示
一下是数组分割小例子:
<?php $names = array("chenlian", "dahan", "chaochao","caixia", "lifang", "xiaoqiang", "hezhun"); $chunkedNames = array_chunk($names, 2, TRUE); for($i=0; $i<count($chunkedNames); $i++) { for ($j=0; $j<count($chunkedNames[$i]); $j++) { echo $i."-".$j.":".$chunkedNames[$i][$i*2+$j]; } } $chunkedNames = array_chunk($names, 2, FALSE); for($i=0; $i<count($chunkedNames); $i++) { for ($j=0; $j<count($chunkedNames[$i]); $j++) { echo $i."-".$j.":".$chunkedNames[$i][$j]; } } ?>
<?php $names = array("chenlian", "dahan", "chaochao","caixia", "lifang", "xiaoqiang", "hezhun"); $chunkedNames = array_chunk($names, 2, TRUE); for($i=0; $i<count($chunkedNames); $i++) { for ($j=0; $j<count($chunkedNames[$i]); $j++) { echo $i."-".$j.":".$chunkedNames[$i][$i*2+$j]; $ret = 0; echo "<pre>"; print_r($chunkedNames[$i], $ret); echo "</pre>"; } } $mergedNames = array_merge($chunkedNames[0],$chunkedNames[1],$chunkedNames[2],$chunkedNames[3]); foreach($mergedNames as $one) { echo "--".$one; } echo "---------------------------<br/>"; $ret = 0; echo "<pre>"; print_r($mergedNames, $ret); echo "</pre>"; ?>
<?php $names = array("chenlian", "dahan", "chaochao","caixia", "lifang", "xiaoqiang", "hezhun"); echo "<br/>nex--".next($names); echo "<br/>pre--".prev($names); echo "<br/>next--".next($names); echo "<br/>next--".next($names); echo "<br/>cur--".current($names); echo "<br/>end--".end($names); echo "<br/>reset--".reset($names); echo "<br/>cur--".current($names); ?>
<?php $names = array("chenlian", "dahan", "chaochao","caixia", "lifang", "xiaoqiang", "hezhun"); echo "original<br/>"; foreach($names as $one) { echo "---".$one; } echo "<br/>"; for($i=0; $i<count($names); $i++) { echo "===".$names[$i]; } echo ksort($names)==TRUE?"TRUE":"FALSE"; echo "<br/>ksort<br/>"; foreach($names as $one) { echo "---".$one; } echo "<br/>"; for($i=0; $i<count($names); $i++) { echo "===".$names[$i]; } echo sort($names)==TRUE?"TRUE":"FALSE"; echo "<br/>sort<br/>"; foreach($names as $one) { echo "---".$one; } echo "<br/>"; for($i=0; $i<count($names); $i++) { echo "===".$names[$i]; } echo rsort($names)==TRUE?"TRUE":"FALSE"; echo "<br/>rsort<br/>"; foreach($names as $one) { echo "---".$one; } echo "<br/>"; for($i=0; $i<count($names); $i++) { echo "===".$names[$i]; } echo asort($names)==TRUE?"TRUE":"FALSE"; echo "<br/>asort<br/>"; foreach($names as $one) { echo "---".$one; } echo "<br/>"; for($i=0; $i<count($names); $i++) { echo "===".$names[$i]; } echo shuffle($names)==TRUE?"TRUE":"FALSE"; echo "<br/>shuffle<br/>"; foreach($names as $one) { echo "---".$one; } echo "<br/>"; for($i=0; $i<count($names); $i++) { echo "===".$names[$i]; } echo array_reverse($names, FALSE)==TRUE?"TRUE":"FALSE"; echo "<br/>array_reverse<br/>"; foreach($names as $one) { echo "---".$one; } echo "<br/>"; for($i=0; $i<count($names); $i++) { echo "===".$names[$i]; } ?>
五.PHP数据类型是不断变化的
<?php $names = array("chenlian", "dahan", "chaochao","caixia", "lifang", "xiaoqiang", "hezhun"); $a = ""; $b = 0; $c = 0.0; echo gettype($c); list($a,$b,$c) = $names; echo "--".$a; echo "--".$b; echo "--".$c; echo gettype($c); $c = 3; echo gettype($c); ?>六.快速创建规律数组
<?php $id = range(1,1000,3); echo "<pre>"; print_r($id); echo "</pre>" ?>七.数组进出
<?php $id = range(1,1000,30); $val = 0; array_push($id, 333); array_push($id, 999); while(($var = array_pop($id)) != NULL){ echo $var."---cnt:".count($id)."<br/>"; } ?>八.字符串索引之大小写转换
<?php //字符串索引之大小写转换 $id = range(1, 6); $id[Hello]="chenlian"; $id = array_change_key_case($id, CASE_UPPER); foreach($id as $one) { echo $one."<br/>"; } echo $id[HELLO]; ?>
<?php $id = range(1,100); $num = range(56,100); $inted = array_intersect($id, $num); echo "<pre>"; print_r($inted); echo "</pre>"; $inted = array_flip($inted); echo "<pre>"; print_r($inted); echo "</pre>"; ?>
十一.字符串
<?php $str = "E:\\OpenSource\\WTL75_5333_Final\\AppWizCE\\\\"; $res = explode("\\", $str, 100); echo "<pre>"; print_r($res); echo "<pre/>"; echo strcmp("abc", "ABC")."abc ABC"."<br/>"; echo "str_replace".str_replace("abc", "CBA", "hello wbabc abc cba xx")."<br/>"; echo "sprintf:".sprintf("%d,%s", 20.35, "travel china")."<br/>"; echo substr("hello chenlian", 0, 8)."<br/>"; echo "trim hello :".trim(" hello ").":"."<br/>"; echo "strlen(hello world)".strlen("hello world")."<br/>"; echo "strtoupper(abAb)".strtoupper("abAb")."<br/>";
echo "<pre>".print_r(str_split("hello world", 2))."</pre>"; ?>
文本化html(不再不被html标签含义)
echo htmlentities("<br/>jehello&&");
hash之
<?php echo md5("hello world")."---".sha1("hello world")."<br/>"; echo md5("hello world")."---".sha1("hello world")."<br/>"; echo md5("hello world")."---".sha1("hello world")."<br/>"; ?>
标签:php
原文地址:http://blog.csdn.net/hellochenlian/article/details/43850669