$a=array( array('key1'=>940,'key2'=>'blah'), array('key1'=>23,'key2'=>'this'), array('key1'=>894,'key2'=>'that') ); print_r($a); function asc_number_sort($x,$y){ if($x['key1']>$y['key1']){ return true; }elseif($x['key1']<$y['key1']){ return false; }else{ return 0; } } usort($a,'asc_number_sort');
$a=array( array('key1'=>940,'key2'=>'blah'), array('key1'=>23,'key2'=>'this'), array('key1'=>894,'key2'=>'that') ); function string_sort($x,$y){ return strcasecmp($x['key2'],$y['key2']); } usort($a,'string_sort');
$students=array( 256=>array('name'=>'John','grade'=>98.5), 2=> array('name'=>'Vance','grade'=>85.1), 9=> array('name'=>'Stephen','grade'=>94.0), 364=>array('name'=>'Steve','grade'=>85.1), 68=> array('name'=>'Rob','grade'=>74.6) ); function name_sort($x,$y){ return strcasecmp($x['name'],$y['name']); } function grade_sort($x,$y){ return ($x['grade']<$y['grade']); } echo '<h2>Array As Is</h2><pre>'.print_r($students,1).'</pre>'; uasort($students,'name_sort'); echo '<h2>Array Sorted By Name</h2><pre>'.print_r($students,1).'</pre>'; uasort($students,'grade_sort'); echo '<h2>Array Sorted By Grade</h2><pre>'.print_r($students,1).'</pre>';
$dbc=mysqli_connect('localhost','root','','php'); if(($_SERVER['REQUEST_METHOD']=='POST')&& !empty($_POST['task'])){ if(isset($_POST['parent_id'])&& filter_var($_POST['parent_id'],FILTER_VALIDATE_INT,array('min_rang'=>1))){ $parent_id=$_POST['parent_id']; }else{ $parent_id=0; } $task=mysqli_real_escape_string($dbc,strip_tags($_POST['task'])); $q="insert into tasks(parent_id,task) values($parent_id,'$task')"; $r=mysqli_query($dbc,$q); if(mysqli_affected_rows($dbc)==1){ echo '<p>The task has been added</p>'; }else{ echo '<p>The task chould not be added!</p>'; } } echo '<form action="add_task.php" method="post"> <fieldset> <legend>Add a Task</legend> <p>Task:<input name="task" type="text" size="60" maxlength="100" required ></p> <p>Parent Task:<select name="parent_id"><option value="0">None</option>'; $q='select task_id,parent_id ,task from tasks where date_completed="0000-00-00 00:00:00" order by date_added ASC'; $r=mysqli_query($dbc,$q); $task=array(); while(list($task_id,$parent_id,$task)=mysqli_fetch_array($r,MYSQL_NUM)){ echo "<option value=\"$task_id\">$task</option>\n"; $tasks[]=array('task_id'=>$task_id,'parent_id'=>$parent_id,'task'=>$task); } echo '</select></p> <input name="submit" type="submit" value="Add This Task"> </fieldset> </form> '; function parent_sort($x,$y){ return ($x['parent_id']>$y['parent_id']); } usort($tasks,'parent_sort'); echo '<h2>Current To-Do List</h2><ul>'; foreach($tasks as $task){ echo "<li>{$task['task']}</li>\n"; } echo '</ul>';
递归函数
静态变量
以引用方式接收值
匿名函数
递归 recursion 就是函数自己调用自己的行为。
<?php function make_list($parent){ global $tasks; echo '<ol>'; foreach($parent as $task_id=>$todo){ echo "<li>".$todo; if(isset($tasks[$task_id])){ make_list($tasks[$task_id]); } echo "</li>"; } echo "</ol>"; } $dbc=mysqli_connect('localhost','root','','php'); $q='select task_id,parent_id,task from tasks where date_completed="0000-00-00 00:00:00" order by parent_id,date_added asc'; $r=mysqli_query($dbc,$q); $tasks=array(); while(list($task_id,$parent_id,$task)=mysqli_fetch_array($r,MYSQLI_NUM)){ $tasks[$parent_id][$task_id]=$task; } echo '<pre>'.print_r($tasks,1).'</pre>'; make_list($tasks[0]);