2013年3月6日星期三

php数组的常用遍历方法



foreach 循环结构.
例题:
$arr = array("hello"=>array(1,2,3,"bbb"=>4,5,6,7,"aaa"=>8),array("one","two"),100=>array("a",1,"b",2));
方法:
foreach($arr as $key=>$value)
{
 foreach($value as $k=>$v)
 {
  echo "$arr[".$key."][".$k."]=".$v."";
 }
 echo "<br>";
}
输出:
$arr[hello][0]=1    $arr[hello][1]=2    $arr[hello][2]=3    $arr[hello][bbb]=4    $arr[hello][3]=5    $arr[hello][4]=6    $arr[hello][5]=7    $arr[hello][aaa]=8    $arr[0][0]=one    $arr[0][1]=two    $arr[100][0]=a    $arr[100][1]=1    $arr[100][2]=b    $arr[100][3]=2
方法:
while(list($key,$value) = each($arr))
{
 while(list($k,$v) = each($value))
 {
  echo $key."==>".$k."===>".$v."<br>";
 }
}
输出:
hello==>0===>1    hello==>1===>2    hello==>2===>3    hello==>bbb===>4    hello==>3===>5    hello==>4===>6   hello==>5===>7
hello==>aaa===>8    0==>0===>one    0==>1===>two    100==>0===>a    100==>1===>1    100==>2===>b    100==>3===>2

php 用方括号的语法新建/修改 数组




可以通过明示地设定值来改变一个现有的数组。

这是通过在方括号内指定键名来给数组赋值实现的。也可以省略键名,在这种情况下给变量名加上一对空的方括号(“[]”)。

$arr[key] = value;
$arr[] = value;
// key 可以是 integer 或 string
// value 可以是任意类型的值
如果 $arr 还不存在,将会新建一个。这也是一种定义数组的替换方法。要改变一个值,只要给它赋一个新值。如果要删除一个键名/值对,要对它用 unset()。

<?php
$arr = array(5 => 1, 12 => 2);

$arr[] = 56;    // This is the same as $arr[13] = 56;
                // at this point of the script

$arr["x"] = 42; // This adds a new element to
                // the array with key "x"
               
unset($arr[5]); // This removes the element from the array

unset($arr);    // This deletes the whole array
?>
Note:

如上所述,如果给出方括号但没有指定键名,则取当前最大整数索引值,新的键名将是该值 + 1。如果当前还没有整数索引,则键名将为 0。如果指定的键名已经有值了,该值将被覆盖。

注意这里所使用的最大整数键名不一定当前就在数组中。它只要在上次数组重新生成索引后曾经存在过就行了。以下面的例子来说明:

<?php
// 创建一个简单的数组
$array = array(1, 2, 3, 4, 5);
print_r($array);

// 现在删除其中的所有元素,但保持数组本身不变:
foreach ($array as $i => $value) {
    unset($array[$i]);
}
print_r($array);

// 添加一个单元(注意新的键名是 5,而不是你可能以为的 0)
$array[] = 6;
print_r($array);

// 重新索引:
$array = array_values($array);
$array[] = 7;
print_r($array);
?>
以上例程会输出:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
Array
(
)
Array
(
    [5] => 6
)
Array
(
    [0] => 6
    [1] => 7
)

PHP Include 文件

服务器端包含 (SSI) 用于创建可在多个页面重复使用的函数、页眉、页脚或元素。

PHP include 和 require 语句

在 PHP 中,您能够在服务器执行 PHP 文件之前把该文件插入另一个 PHP 文件中。
include 和 require 语句用于在执行流中向其他文件插入有用的的代码。
include 和 require 很相似,除了在错误处理方面的差异:
  • require 会产生致命错误 (E_COMPILE_ERROR),并停止脚本
  • include 只会产生警告 (E_WARNING),脚本将继续
因此,如果您希望继续执行,并向用户输出结果,即使包含文件已丢失,那么请使用 include。否则,在框架、CMS 或者复杂的 PHP 应用程序编程中,请始终使用 require 向执行流引用关键文件。这有助于提高应用程序的安全性和完整性,在某个关键文件意外丢失的情况下。
包含文件省去了大量的工作。这意味着您可以为所有页面创建标准页头、页脚或者菜单文件。然后,在页头需要更新时,您只需更新这个页头包含文件即可。

语法

include 'filename';
或者
require 'filename';
PHP include 和 require 语句

基础实例

假设您有一个标准的页头文件,名为 "header.php"。如需在页面中引用这个页头文件,请使用 include/require:
<html>
<body>

<?php include 'header.php'; ?>
<h1>Welcome to my home page!</h1>
<p>Some text.</p>

</body>
</html>

例子 2

假设我们有一个在所有页面中使用的标准菜单文件:
"menu.php":

echo '<a href="/default.php">Home</a>
<a href="/tutorials.php">Tutorials</a>
<a href="/references.php">References</a>
<a href="/examples.php">Examples</a>
<a href="/about.php">About Us</a>
<a href="/contact.php">Contact Us</a>';
网站中的所有页面均应引用该菜单文件。这是具体的做法:
<html>
<body>

<div class="leftmenu">
<?php include 'menu.php'; ?>
</div>

<h1>Welcome to my home page.</h1>
<p>Some text.</p>

</body>
</html>

例子 3

假设我们有一个定义变量的包含文件 ("vars.php"):
<?php
$color='red';
$car='BMW';
?>
这些变量可用在调用文件中:
<html>
<body>

<h1>Welcome to my home page.</h1>
<?php include 'vars.php';
echo "I have a $color $car"; // I have a red BMW
?>

</body>
</html>

PHPCMS V9代码解析: $this->cdb = pc_base::load_model('admin_model');


PHPCMS V9代码解析:
$this->cdb = pc_base::load_model('admin_model');

调用:

/**

* 加载数据模型

* @param string $classname 类名

*/

public static function load_model($classname) {

return self::_load_class($classname,'model');

}


这个时候传输参数‘model’,继续调用_load_class:


/**

* 加载类文件函数

* @param string $classname 类名

* @param string $path 扩展地址

* @param intger $initialize 是否初始化

*/

private static function _load_class($classname, $path = '', $initialize = 1) {

static $classes = array();

if (empty($path)) $path = 'libs'.DIRECTORY_SEPARATOR.'classes';



$key = md5($path.$classname);

if (isset($classes[$key])) {

if (!empty($classes[$key])) {

return $classes[$key];

} else {

return true;

}

}

if (file_exists(PC_PATH.$path.DIRECTORY_SEPARATOR.$classname.'.class.php')) {

include PC_PATH.$path.DIRECTORY_SEPARATOR.$classname.'.class.php';

$name = $classname;

if ($my_path = self::my_path(PC_PATH.$path.DIRECTORY_SEPARATOR.$classname.'.class.php')) {

include $my_path;

$name = 'MY_'.$classname;

}

if ($initialize) {

$classes[$key] = new $name;

} else {

$classes[$key] = true;

}

return $classes[$key];

} else {

return false;

}

}
通过include 引入model文件夹下的源代码类admin_model.

具体源代码如下:

<?php

defined('IN_PHPCMS') or exit('No permission resources.');

pc_base::load_sys_class('model', '', 0);

class admin_model extends model {

public function __construct() {

$this->db_config = pc_base::load_config('database');

$this->db_setting = 'default';

$this->table_name = 'admin';

parent::__construct();

}

}

?>

父类 mode的关键源代码为:

<?php
/**
 *  model.class.php 数据模型基类
 *
 * @copyright (C) 2005-2010 PHPCMS
 * @license http://www.phpcms.cn/license/
 * @lastmodify 2010-6-7
 */
pc_base::load_sys_class('db_factory', '', 0);
class model {
//数据库配置
protected $db_config = '';
//数据库连接
protected $db = '';
//调用数据库的配置项
protected $db_setting = 'default';
//数据表名
protected $table_name = '';
//表前缀
public  $db_tablepre = '';
public function __construct() {
if (!isset($this->db_config[$this->db_setting])) {
$this->db_setting = 'default';
}
$this->table_name = $this->db_config[$this->db_setting]['tablepre'].$this->table_name;

$this->db_tablepre = $this->db_config[$this->db_setting]['tablepre'];
$this->db = db_factory::get_instance($this->db_config)->get_database($this->db_setting);

}

在类db_factory 中使用静态方法获取数据库连接实例,

final class db_factory {
/**
* 当前数据库工厂类静态实例
*/
private static $db_factory;
/** * 数据库配置列表 */
 protected $db_config = array();
 /** * 数据库操作实例化列表 */
 protected $db_list = array();
 /** * 构造函数 */
 public function __construct()
 { }


PHP list() 函数

定义和用法
list() 函数用数组中的元素为一组变量赋值。
注意,与 array() 类似,list() 实际上是一种语言结构,不是函数。

语法

list(var1,var2...)
参数描述
var1必需。第一个需要赋值的变量。
var2可选。可以有多个变量。

提示和注释

注释:该函数只用于数字索引的数组,且假定数字索引从 0 开始。

例子 1

<?php
$my_array = array("Dog","Cat","Horse");

list($a, $b, $c) = $my_array;
echo "I have several animals, a $a, a $b and a $c.";
?>
输出:
I have several animals, a Dog, a Cat and a Horse.

例子 2

<?php
$my_array = array("Dog","Cat","Horse");

list($a, , $c) = $my_array;
echo "Here I only use the $a and $c variables.";
?>
输出:
Here I only use the Dog and Horse variables.

PHP time() 函数



定义和用法

time() 函数返回当前时间的 Unix 时间戳。

语法

time(void)
参数描述
void可选。

说明

返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的秒数。

提示和注释

提示:自 PHP 5.1 起在 $_SERVER['REQUEST_TIME'] 中保存了发起该请求时刻的时间戳。

例子

例子 1

<?php
$t=time();
echo($t . "<br />");
echo(date("D F d Y",$t));
?>
输出:
1138618081
Mon January 30 2006

例子 2

<?php
$nextWeek = time() + (7 * 24 * 60 * 60); // 7 days; 24 hours; 60 mins; 60secs
echo 'Now:       '. date('Y-m-d') ."\n";
echo 'Next Week: '. date('Y-m-d', $nextWeek) ."\n";
?>
输出:
Now:       2005-03-30
Next Week: 2005-04-07