数组处理 int count( mixed var [, int mode] ) 别名:sizeof() 取数组长度 string implode(".", $array) 别名:join 把数组值数据按指定字符连接起来 array explode(".", "fff.ff.f") 按指定字符切割 array range(0, 6, 2) 返回数组 array(0,2,4,6) 第一个参数为起使数,第二个参数为结束数,第三个参数为数据增加步长 void unset ( mixed var [, mixed var [, ...]] ) array array_pad ($a, 5, 's')用's'将数组填补到指定长度 bool shuffle ( array $array ) 将数组打乱 mixed array_rand ( array input [, int num_req] )从数组中随机取出一个或多个单元的索引或键名 array array_count_values ( array input )统计数组中所有的值出现的次数 array array_combine ( array keys, array values ) 创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值 bool array_key_exists ( mixed key, array search )检查给定的键名或索引是否存在于数组中 mixed array_search ( mixed needle, array haystack [, bool strict] )在数组中搜索给定的值,如果成功则返回相应的键名 bool is_array ( mixed var ) bool in_array ( mixed needle, array haystack [, bool strict] )检查数组中是否存在某个值 number array_sum ( array array )计算数组中所有值的和 array array_unique ( array array )移除数组中重复的值 mixed array_shift ( array &array )将数组开头的单元移出数组 int array_unshift ( array &array, mixed var [, mixed ...] )在数组开头插入一个或多个单元 int array_push($a, "3", 1) 把'3'、'1'压入$a,将一个或多个单元压入数组的末尾(入栈),第二个参数开始就是压入的数据 mixed array_pop ( array &array )将数组最后一个单元弹出(出栈) mixed reset ( array &array )将数组的内部指针指向第一个单元 mixed current ( array &array ) 返回数组中的当前单元 值 mixed next ( array &array ) mixed prev ( array &array ) mixed end ( array &array ) 返回数组中的最后一个单元 值 mixed key ( array &array ) 返回数组中的当前个单元 键 array array_keys ( array input [, mixed search_value [, bool strict]] ) 返回数组中所有的键名 array array_values ( array input ) 返回数组中所有的值 array array_splice ( array $input, int offset [, int length [, array replacement]] ) 把数组中的一部分去掉并用其它值取代 array array_merge ( array array1 [, array array2 [, array ...]] )合并一个或多个数组 array array_flip ( array trans )交换数组中的键和值 int extract( array var_array [, int extract_type [, string prefix]] ) 从数组中将变量导入到当前的符号表 array compact ( mixed varname [, mixed ...] ) 建立一个数组,包括变量名和它们的值 bool sort ( array &array [, int sort_flags] )从最低到最高重新安排 bool natsort($a) 用“自然排序”算法对数组排序 bool rsort ( array &array [, int sort_flags] )对数组进行逆向排序(最高到最低) bool asort ( array &array [, int sort_flags] )对数组进行排序并保持索引关系 bool arsort ( array &array [, int sort_flags] ) 对数组进行逆向排序并保持索引关系 bool ksort ( array &array [, int sort_flags] )对数组按照键名排序 bool krsort ( array &array [, int sort_flags] )对数组按照键名逆向排序 usort(array array, callback cmp_function) 使用用户自定义的比较函数对数组中的值进行排序 回调函数返回值作用: 0 不做操作 -1 将$a所在单元先前移动 1 位置不发生变化 如果usort($a ,"cmp"); 中的$a数组只有2个值的情况下,很简单,cmp()第一个参数为$a[1],cmp()第二个参数为$a[0]; 通过cmp比较,返回1则$b>$a , $a[0],$a[1]按原序列排; 返回-1 则反之,$a[0], $a[1]互换位置;返回 0则表示两个数字大小一样,如果是文字的按照第一个字母的ASCII值比较. 如果usort($a ,"cmp"); 中的$a数组超过2个值的情况, 则需要通过递归完成排序 array array_filter ( array input [, callback callback] ) 用回调函数过滤数组中的单元 bool array_walk ( array &array, callback funcname [, mixed userdata] ) 对数组中的每个成员应用用户函数 array array_map ( callback callback, array arr1 [, array ...] )将回调函数作用到给定数组的单元上 array array_fill ( int start_index, int num, mixed value ) 用给定的值填充数组 array_fill(5, 3, 'a')-->array(5=>'a',6=>'a',7=>'a') array array_chunk ( array input, int size [, bool preserve_keys] )将一个数组分割成多个 smarty 模板引擎将不分析 <!--{literal}--> <script> function t() { } </script> <!--{/literal}--> 读取配置文件 <!--{config_load file="config.s"}--> <!--{#site_url#}--> <!--{$smarty.config.site_url}--> 引入文件 <!--{include file="index2.html"}--> <!--{include_php file="/path/to/load_nav.php"}--> $trusted_dir 指定目录下的文件 捕获模板输出的数据 <!--{capture name='eee'}--> fffffffff <!--{/capture}--> <!--{$smarty.capture.eee}--> 循环 <{section name=loop loop=$News_IN}> <{$News_IN[loop].NewsID}> <{/section}> <!--{section name=t loop=$data}--> <tr> <td><!--{$data[t].username}--></td> </tr> <!--{/section}--> <{foreach from=$newsArray item=newsID key=k}> 新闻编号:<{$newsID.newsID}><br> 新闻内容:<{$newsID.newsTitle}><br><hr> <{/foreach}> 判断 <!--{if true}--> 1111 <!--{else}--> 22222222 <!--{/if}--> 时间 {$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"} %Y年%m月%d日 乱码 <!--{$smarty.now|date_format:"%Y年%m月%d日 %H时%M分%S秒"}--> 修改插件:plugins/modifier.date_format.php $format = mb_convert_encoding($format,'gbk','utf-8'); return mb_convert_encoding(strftime($format, $timestamp),'utf-8','gbk'); 局部不缓存 html: <!--{cacheless a="aaa" b="bbbb"}--> <!--{$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}--> <!--{/cacheless}--> php: $smarty->register_block('cacheless', 'smarty_block_dynamic', false);//true:缓存,false:不缓存 function smarty_block_dynamic($param, $content, &$smarty) { return $content; } php: function insert_kk()//方法名前必须有"insert" { return date('Y-m-d H:i:s'); } html: <!--{insert name="kk"}--> 自定义方法 注册方法 php $smarty->register_function('test1', 'test'); function test($p) { return 'ffffffffff'; } html: <!--{test1 name="ff"}--> ------------------------------------------------ 方法自定义 插件文件方式定义方法 function.test.php 文件存在plugins目录下,方法名:smarty_function_test($params, &$smarty) function smarty_function_test($params, &$smarty) { return 'fff'; } html调用: <!--{test name='aa' p='ff'}--> ---------------------------------------------------- 插入方法 插件文件:insert.kk.php文件存于plugins目录下 function smarty_insert_kk() { return date('Y-m-d H:i:s'); } php: function insert_kk()//方法名前必须有"insert" { return date('Y-m-d H:i:s'); } html: <!--{insert name="kk"}--> ------------------------------------------------- 管道符自定义方法 插件文件方式定义方法 modifier.test.php 文件存在于plugins目录下,方法名: function smarty_modifier_test($str, $str2) function smarty_modifier_test($str, $str2) { return $str.$str2; } html调用: <!--{'ff'|test:'tt'}--> php: function eee($a) { return 'ffffffffffffff'; } html: <!--{''|@eee}--> if语句 eq相等, ne、neq不相等, gt大于 gte、ge大于等于, lte、le 小于等于, not非, mod求模。 is [not] div by是否能被某数整除, is [not] even是否为偶数, $a is [not] even by $b 即($a / $b) % 2 == 0 is [not] odd是否为奇 $a is not odd by $b即($a / $b) % 2 != 0 XML sax xml: <--?xml version="1.0" encoding="utf-8"?--> <books> <book> <author>Jack Herrington</author> <title>PHP Hacks</title> <publisher>O'Reilly</publisher> </book> <book> <author>Jack Herrington</author> <title>Podcasting Hacks</title> <publisher>O'Reilly</publisher> </book> <book> <author>作者</author> <title>标题</title> <publisher>出版者</publisher> </book> </books> php: $g_books = array(); $g_elem = null; function startElement( $parser, $name, $attrs ) { global $g_books, $g_elem; if ( $name == 'BOOK' ) $g_books []= array(); $g_elem = $name; } function endElement( $parser, $name ) { global $g_elem; $g_elem = null; } function textData( $parser, $text ) { global $g_books, $g_elem; if ( $g_elem == 'AUTHOR' || $g_elem == 'PUBLISHER' || $g_elem == 'TITLE' ) { $g_books[ count( $g_books ) - 1 ][ $g_elem ] = $text; } } $parser = xml_parser_create(); xml_set_element_handler( $parser, "startElement", "endElement" ); xml_set_character_data_handler( $parser, "textData" ); $f = fopen( '1.xml', 'r' ); while($data = fread( $f, 4096 )) { xml_parse( $parser, $data ); } xml_parser_free( $parser ); foreach( $g_books as $book ) { echo $book['TITLE']." - ".$book['AUTHOR']." - "; echo $book['PUBLISHER']."<br>"; } DomDocument() xml: <--?xml version="1.0" encoding="utf-8"?--> <books> <book> <author>Jack Herrington</author> <title>PHP Hacks</title> <publisher>O'Reilly</publisher> </book> <book> <author>Jack Herrington</author> <title>Podcasting Hacks</title> <publisher>O'Reilly</publisher> </book> <book> <author>作者</author> <title>标题</title> <publisher>出版者</publisher> </book> </books> php读取: $doc = new DOMDocument(); $doc->load( "1.xml"); $books = $doc->getElementsByTagName( "book" ); foreach( $books as $book ) { $authors = $book->getElementsByTagName( "author" ); $author = $authors->item(0)->nodeValue; $publishers = $book->getElementsByTagName( "publisher" ); $publisher = $publishers->item(0)->nodeValue; $titles = $book->getElementsByTagName( "title" ); $title = $titles->item(0)->nodeValue; echo "$title - $author - $publisher<br>"; } php生成: $books = array(); $books [] = array( 'title' => 'PHP Hacks', 'author' => 'Jack Herrington', 'publisher' => "O'Reilly" ); $books [] = array( 'title' => 'Podcasting Hacks', 'author' => 'Jack Herrington', 'publisher' => "O'Reilly" ); $doc = new DOMDocument(); $doc->formatOutput = true; $r = $doc->createElement( "books" ); $doc->appendChild( $r ); foreach( $books as $book ) { $b = $doc->createElement( "book" ); $author = $doc->createElement( "author" ); $author->appendChild($doc->createTextNode( $book['author'] )); $b->appendChild( $author ); $title = $doc->createElement( "title" ); $title->appendChild($doc->createTextNode( $book['title'] )); $b->appendChild( $title ); $publisher = $doc->createElement( "publisher" ); $publisher->appendChild($doc->createTextNode( $book['publisher'] )); $b->appendChild( $publisher ); $r->appendChild( $b ); } echo $doc->saveXML(); echo $doc->save('222.xml'); SimpleXML xml: <books> <book> <author>Jack Herrington</author> <title>PHP Hacks</title> <publisher>O'Reilly</publisher> </book> </books> php: $xml = new SimpleXMLElement('1.xml', NULL, TRUE); echo $xml->book[0]->author."___".$xml->book[0]->title."___".$xml->book[0]->publisher; |