Archive for the ‘ PHP ’ Category

深入理解Magento – 第四章 – 模型和ORM基础

对于任何一个MVC架构,模型(Model)层的实现都是占据了很大一部分。对于Magento来说,模型占据了一个更加重要的位置,因为它常常包含了一部分商业逻辑代码(可以说它对,也可以说它错)。这些代码在其他的MVC框架中往往出现在控制器或者帮助函数中。

传统的PHP MVC架构中的模型

本来MVC的定义就不是很清晰,不同的人有不同的看法,而对于模型的定义争议就更多了。在MVC模式被广泛采用之前,PHP程序员往往通过SQL语句直接操作数据库。也有些程序员通过一个SQL抽象层来操作数据库(比如AdoDB)。程序员往往关注SQL语句本身,而不是和数据相关的对象。

虽然直接操作SQL的方式一直被病诟,但是很多PHP框架还是以SQL为中心的。模型层提供了一系列对象,抽象/封装了数据操作,但是程序员最终还是需为模型层对象写SQL语句操作数据库。

阅读全文

深入理解Magento – 第三章 – Magento的布局(Layout),块(Block)和模板(Template)

我们接着研究Magento。根据我们第二章讲的Magento MVC的架构,我们接下来应该讲模型(Model),但是我们跳过模型先来看布局和块。和一些流行的PHP MVC架构不同的是,Magento的执行控制器不直接将数据传给试图,相反的视图将直接引用模型,从模型取数据。这样的设计就导致了视图被拆分成两部分,块(Block)和模板(Template)。块是PHP对象,而模板是原始PHP文件,混合了XHTML和PHP代码(也就是把PHP作为模板语言来使用了)。每一个块都和一个唯一的模板文件绑定。在模板文件phtml中,“$this”就是指该模板文件对应的快对象。

让我们来看一个例子

File: app/design/frontend/base/default/template/catalog/product/list.phtml

你将看到如下代码

< ?php $_productCollection=$this->getLoadedProductCollection() ?>
< ?php if(!$_productCollection->count()): ?>
<p class="note-msg">< ?php echo $this->__('There are no products matching the selection.') ?></p>
< ?php else: ?>

这里“getLoadedProductCollection”方法可以在这个模板的块对象“Mage_Catalog_Block_Product_List”中找到

File: app/code/core/Mage/Catalog/Block/Product/List.php
...
public function getLoadedProductCollection()
{
    return $this->_getProductCollection();
}   
...

块的“_getProductCollection”方法会实例化模型,并读取数据然后返回给模板。

阅读全文

深入理解Magento – 第二章 – Magento请求分发与控制器

Model-View-Controller (MVC) ,模型-视图-控制器,源于Smalltalk编程语言和Xerox Parc。现在有很多系统是基于MVC架构的,不同的系统MVC的实现也略有不同,但都体现了MVC的精髓,分离数据,业务逻辑和显示逻辑。最常见的PHP MVC框架是这样的

  1. URL请求被一个PHP文件拦截,通常称为前端控制器(Front Controller)
  2. 这个PHP文件分析这个URL,获得一个执行控制器(Action Controller)的名字和一个执行方法(Action Method)的名字,这个过程通常称为路由(Routing)
  3. 实例化#2获得的执行控制器
  4. 调用执行控制器的执行方法
  5. 执行方法中处理业务逻辑,比如获取数据
  6. 执行控制器负责把数据传递给显示逻辑
  7. 显示逻辑生成HTML

阅读全文

深入理解Magento – 第一章 – Magento强大的配置系统

Magento的配置系统就像是Magento的心脏,支撑着Magento的运行。这套配置系统掌管着几乎所有“module/model/class/template/etc”。它把整个Magento系统抽象出来,用一个配置文件来描述。这里的“配置文件”并不是一个物理上存在的文件,而是Magento根据当前的系统状态动态生成的一段XML。大多数的PHP开发者并不习惯于这样抽象层,因为它增加的编程的复杂性。但是这样的抽象提供了无与伦比的灵活性,允许你覆盖几乎任何系统的默认行为。

首先,让我们写一个简单的插件来看看这个所谓的“配置文件”长什么样。虽然我已经提供的现成的代码,但是还是建议你自己建立这个插件,把整个流程走一遍有助于你的理解。

阅读全文

PHP获取MSN好友列表类

<?php
 
error_reporting(7);
 
class msn
 
{
 
    private $startcomm = 0;
 
    private $username = '';
 
    private $password = '';
 
    private $commend = '';
 
    private $domain = '';
 
    private $socket = '';
 
    private $challenge = '';
 
    private $status = array();
 
    private $data = array();
 
 
 
    function set_account($username, $password)
 
    {
 
        $this->username = $username;
 
        $this->password = $password;
 
    }
 
 
 
    function getData(){
 
        $buffer="";
 
        while (!feof($this->socket)) {
 
            $buffer .= fread($this->socket,1024);
 
            if (preg_match("/\r/",$buffer)) {
 
                break;
 
            }
 
        }
 
        $this->checkData($buffer);
 
    }
 
 
 
    function getData2() {
 
        $buffer="";
 
        while (!feof($this->socket)) {
 
            $buffer .= fread($this->socket,1024);
 
            if (preg_match("/\r\n\r\n/",$buffer)) {
 
                break;
 
            }
 
        }
 
        $this->checkData($buffer);
 
    }
 
 
 
    function checkData($buffer) {
 
        if (preg_match("/lc\=(.+?)/Ui",$buffer,$matches)) {    
 
            $this->challenge = "lc=" . $matches[1];
 
        }
 
 
 
        if (preg_match("/(XFR 3 NS )([0-9\.\:]+?) (.*) ([0-9\.\:]+?)/is",$buffer,$matches)) {
 
            $split = explode(":",$matches[2]);
 
            $this->startcomm = 1;
 
            $this->msn_connect($split[0],$split[1]);
 
        }
 
 
 
        if (preg_match("/tpf\=([a-zA-Z0-9]+?)/Ui",$buffer,$matches)) {
 
            $this->nexus_connect($matches[1]);
 
        }
 
 
 
        $split = explode("\n",$buffer);
 
        for ($i=0;$i<count($split);$i++) {  
 
            $detail = explode(" ",$split[$i]);
 
            if ($detail[0] == "LST") {
 
                if(isset($detail[2])) $this->data[] = array($detail[1], urldecode($detail[2]));
 
            }
 
        }
 
        $this->status = array(200, $this->data);
 
        //echo $buffer;
 
    }
 
 
 
    function msn_connect($server,$port) {
 
        if ($this->socket) {
 
            fclose($this->socket);
 
        }
 
        $this->socket = @fsockopen($server,$port, $errno, $errstr, 20);
 
        if (!$this->socket) {
 
            $this->status = array(500,'MSN验证服务器无法连接');
 
            return false;
 
        } else {
 
            $this->startcomm++;
 
            $this->send_command("VER " . $this->startcomm . " MSNP8 CVR0",1);
 
            $this->send_command("CVR " . $this->startcomm . " 0x0409 win 4.10 i386 MSNMSGR 6.2 MSMSGS " . $this->username,1);
 
            $this->send_command("USR " . $this->startcomm . " TWN I " . $this->username,1);
 
        }
 
    }
 
 
 
    function send_command($command) {
 
        $this->commend = $command;
 
        $this->startcomm++;       
 
        fwrite($this->socket,$command . "\r\n");
 
        $this->getData();
 
    }
 
 
 
    function nexus_connect($tpf) {
 
        $arr[] = "GET /rdr/pprdr.asp HTTP/1.0\r\n\r\n";
 
        $curl = curl_init();
 
        curl_setopt($curl, CURLOPT_URL, "https://nexus.passport.com:443/rdr/pprdr.asp");
 
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 
        curl_setopt($curl, CURLOPT_VERBOSE, 0);
 
        curl_setopt($curl, CURLOPT_HEADER,1);
 
        curl_setopt($curl, CURLOPT_HTTPHEADER, $arr);
 
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
 
        $data = curl_exec($curl);
 
        curl_close($curl);
 
        preg_match("/DALogin=(.+?),/",$data,$matches);
 
        if(!isset($matches[1])) return false;
 
        $split = explode("/",$matches[1]);
 
        $headers[0] = "GET /$split[1] HTTP/1.1\r\n";
 
        $headers[1] = "Authorization: Passport1.4 OrgVerb=GET,OrgURL=http%3A%2F%2Fmessenger%2Emsn%2Ecom,sign-in=" . $this->username . ",pwd=" . $this->password . ", " . trim($this->challenge) . "\r\n";
 
        $curl = curl_init();
 
        curl_setopt($curl, CURLOPT_URL, "https://" . $split[0] . ":443/". $split[1]);
 
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 
        curl_setopt($curl, CURLOPT_VERBOSE, 0);
 
        curl_setopt($curl,CURLOPT_FOLLOWLOCATION,1);
 
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
 
        curl_setopt($curl, CURLOPT_HEADER,1);
 
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
 
        $data = curl_exec($curl);
 
        curl_close($curl);
 
        preg_match("/t=(.+?)'/",$data,$matches);
 
        if(!isset($matches[1])){
 
            $this->status = array(404, '你输入的MSN帐号或者密码错误');
 
            return false;
 
        }
 
        $this->send_command("USR " . $this->startcomm . " TWN S t=" . trim($matches[1]) . "",2);
 
        $this->send_command("CHG " . $this->startcomm . " HDN",2);
 
        $this->send_command("SYN " . $this->startcomm . " 0",2);
 
        $this->getData2();
 
        $this->send_command("SYN " . $this->startcomm . " 1 46 2",2);
 
        $this->getData2();
 
        $this->send_command("CHG ". $this->startcomm . " BSY");
 
        $this->getData();     
 
    }
 
 
 
    public function getStatus()
 
    {
 
        return $this->status;
 
    }
 
}
 
 
 
 
 
$msn = new MSN;
 
$msn->set_account('xx@hotmail.com', 'xxxxx');
 
$msn->msn_connect("messenger.hotmail.com",1863);
 
$data = $msn->getStatus();
 
print_r($data);
?>

有趣的Nginx QUERY_STRING BUG

今天在开发一个验证签名的功能,所以需要从$_SERVER['QUERY_STRING']截取到签名前的字符串。但是开发环境中是APACHE的,生产环境是NGINX+PHP_FASTCGI,问题就从这里来了,请看下面。

NGINX下的QUERY_STRING:
[QUERY_STRING] => gateway/login/&&MemberID=110000009569680&EmpFlg=N&UserName=LIJOSHUA&EmpOrgName=&sysDate=03%2F25%2F2010+10%3A55%3A30&paSignature=6CA0B32525DEEF5A4571AE2CB7D16C326E1636E08367F8658B422B279FFA787C9206F0FFBFF1D876E5602928E0F745FB83856D12FE3DF2C5EF28A4BE1EF15D7DB6F1E0A4C841F0029F6463C0483E761789F1C9

APACHE下的QUERY_STRING:
[QUERY_STRING] => MemberID=110000009569680&EmpFlg=N&UserName=LIJOSHUA&EmpOrgName=&sysDate=03%2F25%2F2010+10%3A55%3A30&paSignature=6CA0B32525DEEF5A4571AE2CB7D16C326E1636E08367F8658B422B279FFA787C9206F0FFBFF1D876E5602928E0F745FB83856D12FE3DF2C5EF28A4BE1EF15D7DB6F1E0A4C84

用法修改:
$query_string = $_SERVER['QUERY_STRING'];
改为:
$uri = $_SERVER['REQUEST_URI'];
$arrURL = parse_url($uri);
$query_string = $arrURL['query'];

Linux安装Nagios监控系统, perl-fcgi, nginx

作者:Joshua
日期:2009年1月17日凌晨
非请勿转
安装nagios
与别的软件安装稍有不同,nagios的安装要好几步才能完成。

第一步,执行make install安装主要的程序、CGI及HTML文件;
第二步,执行 make install-commandmode 给外部命令访问nagios配置文件的权限;
第三步,执行 make install-config 把配置文件的例子复制到nagios的安装目录。

按照安装向导的提示,其实这里还有一个 make install-init的步骤,它的作用是把nagios做成一个运行脚本,使nagios随系统开机启动,这是一个很方便的措施。

cd /usr/local/
wget http://nchc.dl.sourceforge.net/sourceforge/nagios/nagios-3.0.6.tar.gz
tar xzf nagios-3.0.6.tar.gz
cd nagios-3.0.6
/usr/sbin/useradd nagios
passwd nagios
/usr/sbin/groupadd nagcmd
/usr/sbin/usermod -G nagcmd nagios
./configure --with-command-group=nagcmd
make all
make install
make install-init
make install-config
make install-commandmode
vi /usr/local/nagios/etc/objects/contacts.cfg //修改管理员邮件地址

验证Nagios安装

验证程序是否被正确安装。切换目录到安装路径(这里是/usr/local/nagios),看是否存在 etc、bin、 sbin、 share、 var这五个目录,如果存在则可以表明程序被正确的安装到系统了。五个目录功能的简要说明:

bin Nagios执行程序所在目录,这个目录只有一个文件nagios
etc Nagios配置文件位置,初始安装完后,只有几个*.cfg-sample文件
sbin Nagios Cgi文件所在目录,也就是执行外部命令所需文件所在的目录
Share Nagios网页文件所在的目录
Var Nagios日志文件、spid 等文件所在的目录

安装nagios的插件

没有插件,nagios将什么作用也没有,插件也是nagios扩展功能的强大武器,除了下载常用的插件外,我们还可以根据实际要求编写自己的插件。 Nagios的插件nagios-plugins-1.4.5在www.nagios.org上可以找到,接着我们用wget下载它。注意:插件与 nagios之间的版本关联不大,不一定非得用nagios-plugins-1.4.5这个版本。下载完成后,安装它是很简单的:先执行配置 ./configure –prefix=/usr/local/nagios ,接着编译安装 make ;make install即可。这里需要说明一下的是在配置过程指定的安装路径是/usr/local/nagios,而不是/usr/local/nagios- plus,安装完成后,将在目录/usr/local/nagios生成目录libexec(里面有很多文件),这正是nagios所需要的。

wget http://jaist.dl.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.13.tar.gz
tar xf nagios-plugins-1.4.13.tar.gz
cd nagios-plugins-1.4.13
./configure --with-nagios-user=nagios --with-nagios-group=nagcmd
make
make install

添加Nagios随系统启动

/sbin/chkconfig --add nagios
/sbin/chkconfig nagios on
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
/sbin/service nagios start

安装perl fcgi模块

      wget http://www.cpan.org/modules/by-module/FCGI/FCGI-0.67.tar.gz
      tar -zxvf FCGI-0.67.tar.gz
      cd FCGI-0.67
      perl Makefile.PL
      make && make install

安装FCGI-ProcManager:

      wget http://search.cpan.org/CPAN/authors/id/G/GB/GBJK/FCGI-ProcManager-0.18.tar.gz
      tar -xzxf FCGI-ProcManager-0.18.tar.gz
      cd FCGI-ProcManager-0.18
      perl Makefile.PL
      make
      make install
cd /usr/local/nagios/bin/
vi perl-cgi.pl
#!/usr/bin/perl
 
use FCGI;
#perl -MCPAN -e 'install FCGI'
use Socket;
 
#this keeps the program alive or something after exec'ing perl scripts
END() { } BEGIN() { }
*CORE::GLOBAL::exit = sub { die "fakeexit\nrc=".shift()."\n"; }; eval q{exit}; if ($@) { exit unless $@ =~ /^fakeexit/; } ;
 
&main;
 
sub main {
                #$socket = FCGI::OpenSocket( ":3461", 10 ); #use IP sockets
                $socket = FCGI::OpenSocket( "/var/run/nagios.sock", 10 ); #use UNIX sockets - user running this script must have w access to the 'nginx' folder!!
                $request = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%ENV, $socket );
                if ($request) {request_loop()};
                        FCGI::CloseSocket( $socket );
}
 
sub request_loop {
                while( $request->Accept() >= 0 ) {
 
                   #processing any STDIN input from WebServer (for CGI-GET actions)
                   $env = $request->GetEnvironment();
                   $stdin_passthrough ='';
                   $req_len = 0 + $ENV{CONTENT_LENGTH};
                   if ($ENV{REQUEST_METHOD} eq 'GET'){
                                $stdin_passthrough .= $ENV{'QUERY_STRING'};
                        }
 
                        #running the cgi app
                        if ( (-x $ENV{SCRIPT_FILENAME}) && #can I execute this?
                                 (-s $ENV{SCRIPT_FILENAME}) && #Is this file empty?
                                 (-r $ENV{SCRIPT_FILENAME})     #can I read this file?
                        ){
                                #http://perldoc.perl.org/perlipc.html#Safe-Pipe-Opens
                open $cgi_app, '-|', $ENV{SCRIPT_FILENAME}, $stdin_passthrough or print("Content-type: text/plain\r\n\r\n"); print "Error: CGI app returned no output - Executing $ENV{SCRIPT_FILENAME} failed !\n";
                                if ($cgi_app) {print <$cgi_app>; close $cgi_app;}
                        }
                        else {
                                print("Content-type: text/plain\r\n\r\n");
                                print "Error: No such CGI app - $req_len - $ENV{CONTENT_LENGTH} - $ENV{REQUEST_METHOD} - $ENV{SCRIPT_FILENAME} may not exist or is not executable by this process.\n";
                        }
 
                }
}
vi start_ngind_cgi.sh
#!/bin/bash
 
## start_nginx_cgi.sh: start nginx cgi mode
## ljzhou, 2007.08.20
 
 
PERL="/usr/bin/perl"
NGINX_CGI_FILE="/usr/local/nagios/bin/perl-cgi.pl"
 
 
#bg_num=`jobs -l |grep "NGINX_CGI_FILE"`
#PID=`ps aux|grep "perl-cgi"|cut -c10-14|xargs kill -9`
PID=`ps aux|grep 'perl-cgi'|cut -c10-14|sed -n "1P"`
echo $PID
sockfiles="/var/run/nagios.sock"
kill -9 $PID
 
$PERL $NGINX_CGI_FILE &
 
sleep 3
 
`chown nobody.nobody $sockfiles`

创建身份认证文件
注意:这里的用户既是apache的nagios管理界面的登录认证用户也和nagios监控中的权限有关联。

htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin

修改nginx.conf文件,在虚拟机中加入:

        location /nagios {
                auth_basic              "Restricted";
                auth_basic_user_file    /usr/local/nagios/etc/htpasswd.users;
        }
        location ~ \.cgi$ {
                root /usr/local/nagios/sbin;
                rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
                fastcgi_index index.cgi;
                fastcgi_pass    unix:/var/run/nagios.sock;
 
                fastcgi_param   SCRIPT_FILENAME                     /usr/local/nagios/sbin$fastcgi_script_name;
                fastcgi_param   QUERY_STRING                        $query_string;
 
                fastcgi_param   REMOTE_ADDR                        $remote_addr;
                fastcgi_param   REMOTE_PORT                        $remote_port;
                fastcgi_param   REQUEST_METHOD                     $request_method;
                fastcgi_param   REQUEST_URI                        $request_uri;
 
                #fastcgi_param SCRIPT_NAME                        $fastcgi_script_name;
                fastcgi_param   SERVER_ADDR                        $server_addr;
                fastcgi_param   SERVER_NAME                        $server_name;
                fastcgi_param   SERVER_PORT                        $server_port;
                fastcgi_param   SERVER_PROTOCOL                    $server_protocol;
                fastcgi_param   SERVER_SOFTWARE                    nginx;
 
                fastcgi_param   CONTENT_LENGTH                     $content_length;
                fastcgi_param   CONTENT_TYPE                       $content_type;
                fastcgi_param   GATEWAY_INTERFACE                  CGI/1.1;
                fastcgi_param   HTTP_ACCEPT_ENCODING        gzip,deflate;
                fastcgi_param   HTTP_ACCEPT_LANGUAGE        zh-cn;
       }

之后运行/usr/local/nagios/bin/start_nginx_cgi.sh,并将它加到/etc/rc.local中。

/usr/local/nagios/bin/start_nginx_cgi.sh
vi /etc/rc.local ///usr/local/nagios/bin/start_nginx_cgi.sh写在最后一行

重启nginx:

ps -aux|grep nginx
kill -HUP pid //此处的pid是ps查出的nginx主进程id

最后要把nagios的share文件放到虚拟主机根目录下,并将share改名为nagios,最佳办法是:

cd 你的虚拟主机根目录
ln -s /usr/local/nagios/share nagios

访问:http://yourhost.com/nagios
如果看到根我一样的界面,那就对了,图片点击可放大。

nagios成功安装

nagios成功安装

Only Love

If you love him or her, please let he or her know and do not …

“When I heard this, I froze. My tantrum killed him. Just because of an unmailed card, he died. After that, I lost my privilege to be childish.”

It’s a cold February night. People are bustling through the streets, either pulling up their coat collars or wrapping scarves around their necks, trying to stay warm.

It’s so cold today. I’m standing at my window, looking at the people moving like little dots. Standing in a heated room, I’m beginning to pity those people. Why don’t they go home? Do they plan to wandering until morning?

“Almost time to go home! My  boyfriend must be going crazy.” One of the nurses breathe a sign of relief.” Still needs to work overtime on Valentine’s Day. It’s so unfair!”

“You are fortunate.” Another nurse says. “Some people don’t have anyone waiting for them.”

“You mean Dr.Shu?”

阅读全文

网络搜索点燃战火 明争暗斗再度升级 ! 你中有我,我中有你

网络搜索点燃战火 明争暗斗再度升级
百度、谷歌、淘宝、星球导航4大巨头亮剑出鞘
。。。。。。
。。。。。。

您瞧:当键入http: //www.baidumeiyou.com(百度没有)——打开网页竟然是谷歌;
键入http://www.taobaomeiyou.com(淘宝没有)——打开网页却是百度有啊;
键入http://www.Baiduyouma.com(百度有吗)——打开网页是淘宝;
而键入http: //www.Googleyouma.com(谷歌有吗)——打开网址竟然是星球导航! 

好一个“你中有我,我中有你”啊!互联网的争 端真可谓层出不穷,当大家还在热烈争论网络域名何去何从时,巨头们已把市场的无形利刃插进了对手的心脏!记得几年前,当杨利伟和神舟五号还没有摆脱地心引 力飞进太空的时候,以“杨利伟”和“神舟五号”似字眼申报的网络域名已经被抢注一空,域名抢注已经成为互联网发展的一大景观,只要和现实事件有关的一些关 键词,都有可能被抢注为网络域名,成为经营者的商标。只要在互联网搜索,就可能随时会查到一些令人啼笑皆非的搞笑域名:“央视一套”——实为避孕套;“俯 卧撑”——实为床上用品;“三聚情安”——实为休闲会所名:“结石”——实为健身器材……甚至还拿领导人的谐音来抢注自己的网络商业域名。

PHP操作数组相关函数

range($low, $high),range($low, $high, $step);//创建顺序值的数组如:range(1,4)为(1,2,3,4)又如range(‘a’,'z’)

each($array)按顺序返回数组的当前元素,并且将下一个元素设置为当前元素;

reset($array)将数组当前元素重新设置到数组开始处

list()可以用来将一个数组分解为一系列的值,如 list($a,$b)=each($array)

shuffle($array),array_rand($arg, $num_req);对数组随机排序

array_reverse($input),array_reverse($input, $preserve_keys) 返回原数组的反向排序

sort($array);对数组排序

PHP数组是一个重要的概念,它包含有大量的函数,方便人们的开发…现将它的数组分类,以方便查询及应用.
先说说PHP数组的定义…PHP数组包含两个项,key和value,可以通过key来获取相应的value,其中key又可以是数值和关联的,如$array[0],$array[one]…
创建数组
PHP中的数组声明跟其它语言的也有点小小的差别,但一样可以声明为一维,两维,三维及多维等,如
$array[0] = 1,$array = array(1,2,3); 一维数组,只包括三个值,属于数值型数组,引用时可用$array[0]来代表1,创建数值数组时可以省略索引.

$array = array(
   1 => “one”,
   2 => “two”,
   3 => “three”,
   4 => array(
    “one” => 1,
    “two” => 2,
    “three” => 3
   )
);

二维数组,同时又是关联数组,引用时可以$array[4][“one”]来代表1.
三维以上依此类推…
如果要批量创建数组,则可以通过下面这个函数:

array range ( mixed low, mixed high [, number step] )

如$array = range(1,6);代表array(1,2,3,4,5,6);
$array = range(a,f); 代表 array(a,b,c,d,e,f);

输出数组
PHP中输出数组的函数有比较多,常用的有
bool print_r ( mixed expression [, bool return] )
void var_dump ( mixed expression [, mixed expression [, ...]] )
还有像echo,print,printf都可以输出单个数组.

测试数组
有时我们需要判定一个变量是否为数组,则可以使用:
bool is_array ( mixed var )

增加或删除数组元素
数组声明后并不是一成不变的,可能通过对数组的增加删除来进行深入的操作:
int array_push ( array &array, mixed var [, mixed ...] ) 将一个或多个单元压入数组的末尾,数组的长度根据入栈变量的数目增加,如array_push($array,$var)
mixed array_pop ( array &array ) 将数组的最后一个元素弹出(出栈),并在结束后重置数组的指针
mixed array_shift ( array &array ) 返回数组的第一个元素.
int array_unshift ( array &array, mixed var [, mixed ...] ) 在数组的开头插入一个或多个单元
array array_pad ( array input, int pad_size, mixed pad_value ) 用值将数组填补到指定的长度,如array_pad($array,3,$var);

定位数组元素
bool in_array ( mixed needle, array haystack [, bool strict] ) 检查数组中是否存在某个值
array array_keys ( array input [, mixed search_value [, bool strict]] ) 返回数组中的所有键名,重组成一个新数组
bool array_key_exists ( mixed key, array search ) 检查给定的key是否存在于数组中.
array array_values ( array input ) 返回数组中所有的值
mixed array_search ( mixed needle, array haystack [, bool strict] ) 在数组中搜索给定的值,成功则返回key.

遍历数组
PHP中提供了很多获取key和value的函数
mixed key ( array &array ) 从关联数组中取得键名
mixed reset ( array &array ) 将数组指针重置
array each ( array &array ) 返回数组中的键/值对并将数组向前移一步
mixed current ( array &array ) 返回数组中的当前单元
mixed end ( array &array ) 将数组中的指针移向最后一位
mixed next ( array &array ) 将数组中的指针移向下一位
mixed prev ( array &array ) 将数组中的指针移向上一位
array array_reverse ( array array [, bool preserve_keys] ) 返回一个单元顺序相反的数组
array array_flip ( array trans ) 将数组中的键值角色调换
除了上面的函数外还可以使用循环来对数组中的元素进行遍历,如
foreach (array_expr as $value)
{ statement }
foreach (array_expr as $key=>$value)
{ statement }
提取每个键/值对,直到获得所有项或满足某些内部条件为止
void list ( mixed varname, mixed … ) 把数组中的值赋给一些变量

确定数组大小和唯一性
int count ( mixed var [, int mode] ) 计算数组中单元数组或对象中属性的个数, sizeof 的同名函数
array array_count_values ( array input ) 统计数组中所有值出现的次数
array array_unique ( array array ) 移除数组中重复的值

数组排序
这个听说是计算器的核心问题…呵呵…事实也是这样…
bool sort ( array &array [, int sort_flags] ) 对数组进行排序
bool natsort ( array &array ) 用自然排序法对数组进行排序
bool natcasesort ( array &array ) 用自然排序法对数组进行排序,不区分大小写
bool rsort ( array &array [, int sort_flags] ) 对数组进行逆向排序
bool asort ( array &array [, int sort_flags] ) 对数组进行排序并保持索引关系
bool array_multisort ( array ar1 [, mixed arg [, mixed ... [, array ...]]] ) 对多个数组或多维数组进行排序
bool arsort ( array &array [, int sort_flags] ) 对数组进行逆序排序并保持索引关系
bool ksort ( array &array [, int sort_flags] ) 对数组按键名排序
bool krsort ( array &array [, int sort_flags] ) 对数组按键名逆序排序

合并,拆分,接合和分解数组
array array_combine ( array keys, array values ) 创建一个数组,一个数组的值作为其键名,另一个数组的值作为其值
array array_merge ( array array1 [, array array2 [, array ...]] ) 合并一个或多个数组
array array_merge_recursive ( array array1 [, array ...] ) 递归地全部一个或多个数组
array array_slice ( array array, int offset [, int length [, bool preserve_keys]] ) 从数组中取出一段,建立一个新的数组,如果offset为正数,拆分从距数组开关的offset位置开始,如果为负数,则拆分从距数组末尾的offset 位置开始,此时距数组开关的count(input_array)-|length|位置结束
array array_splice ( array &input, int offset [, int length [, array replacement]] ) 把数组中的部分值去掉,并用其它值替代.offset设置同上
array array_intersect ( array array1, array array2 [, array ...] ) 计算数组的交集,即是说如果第一个数组中出现过的值在接下来的几个数组中都有出现,则取出该值
array array_intersect_assoc ( array array1, array array2 [, array ...] ) 带索引检查数组中的交集
array array_intersect_key ( array array1, array array2 [, array ...] ) 使用键名比较数组中的交集
array array_diff ( array array1, array array2 [, array ...] ) 计算数组的差集, 即是说跟第一个数组中不同的值
array array_diff_assoc ( array array1, array array2 [, array ...] ) 带索引检查数组中的差集
array array_diff_key ( array array1, array array2 [, array ...] ) 使用键名比较数组中的差集

其它比较有用的数组函数
数组函数还有好多没有列出来…再上几个比较有用也比较常的,其它的就参考手册啦…手册里很清楚
mixed array_rand ( array input [, int num_req] ) 数组中随机取出一个或多个键,num指定个数
bool shuffle ( array &array ) 将数组打乱
number array_sum ( array array ) 计算数组中所有值的总和,关联数组忽略
array array_chunk ( array input, int size [, bool preserve_keys] ) 将一个数组分割成几个