继上一篇文章:PHP二维数组排序自定义函数,今天,我们再介绍一个php二维数组排序的例子。
php对二维数组的排序很简单,主要用到array_multisort()函数。
例子:
<?php /** * php二维数组排序 * edit www. */ $data = array(); $data[] = array('volume' => 67, 'edition' => 2); $data[] = array('volume' => 86, 'edition' => 1); $data[] = array('volume' => 85, 'edition' => 6); $data[] = array('volume' => 98, 'edition' => 2); $data[] = array('volume' => 86, 'edition' => 6); $data[] = array('volume' => 67, 'edition' => 7); // 取得列的列表 foreach ($data as $key => $row) { $volume[$key] = $row['volume']; $edition[$key] = $row['edition']; } array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data); print_r($data); ?>
输出结果:
Array
(
[0] => Array
(
[volume] => 98
[edition] => 2
)
[1] => Array
(
[volume] => 86
[edition] => 1
)
[2] => Array
(
[volume] => 86
[edition] => 6
)
[3] => Array
(
[volume] => 85
[edition] => 6
)
[4] => Array
(
[volume] => 67
[edition] => 2
)
[5] => Array
(
[volume] => 67
[edition] => 7
)
)
说明:
array_multisort函数的参数非常灵活,大家可以参照php手册中的说明,深入研究下。
一起学习下php数组函数:array_keys函数与array_values()函数的用法。
例1:
<?php /** * 输出php数组的键名与键值 * edit www. */ $arr=array( "one" => "php", "two" => "java", "three" => array("a" => "apple","b" => "bananner") ); echo '<pre />'; print_r(array_keys()($arr));//输出键名 echo '<pre />'; print_r(array_values($arr));//输出键值 ?>
说明:
array_keys() 函数返回包含数组中所有键名的一个新数组。
例2,使用 value 参数:
<?php $a=array("a"=>"Horse","b"=>"Cat","c"=>"Dog"); print_r(array_keys($a,"Dog")); ?>
输出:
Array ( [0] => c)
例3,使用 strict 参数 (false) :
<?php $a=array(10,20,30,"10"); print_r(array_keys($a,"10",false)); ?>
输出:
Array ( [0] => 0 [1] => 3 )
说明:
array_values()函数返回一个包含给定数组中所有键值的数组,但不保留键名。
在php编程调试时,经常需要模拟提交。
另外,在抓取一些页面时,需要经常请求别人的页面。
于是实现了一个http请求的封装类,以方便调用。
本http请求类,封装了三种post提交方法和一个request请求方法。
1,http请求封闭类
<?php /** * HTTP常用请求封装 * @version $Id: HttpHelper.php,v 1.0 2012-8-9 * @package library * @site www. */ // --------------------------- /** * http请求处理 * * 开发中经常需要模拟提交请求,本类封装了常用的post方法 * * @author ustb80 * */ class HttpHelper { // 当前的user-agent字符串 public $ua_string= "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:14.0) Gecko/20100101 Firefox/14.0.1"; // 支持的提交方式 public $post_type_list = array("curl", "socket", "stream"); // 本地cookie文件 private $cookie_file; // ----------------------- /** * 构造函数 * * @param array $params 初始化参数 */ public function __construct($params = array()) { if(count($params) > 0) { $this->init($params); } } // ----------------------- /** * 参数初始化 * * @param array $params */ public function init($params) { if(count($params) > 0) { foreach($params as $key => $val) { if(isset()($this->$key)) { $this->$key = $val; } } } } // ----------------------- /** * 提交请求 * * @param string $url 请求地址 * @param mixed $data 提交的数据 * @param string $type 提交类型,curl,socket,stream可选 */ public function post($url, $data, $type = "socket") { if(!in_array($type, $this->post_type_list)) { die("undefined post type"); } $function_name = $type . "Post"; return call_user_func_array(array($this, $function_name), array($url, $data)); } // ----------------------- /** * 更改默认的ua信息 * * 本方法常用于模拟各种浏览器 * * @param string $ua_string UA字符串 */ public function setUA($user_agent) { $this->ua_string = $user_agent; return $this; } // ----------------------- /** * 设置本地cookie文件 * * 在用curl来模拟时常需要设置此项 * * @param string $cookie_file 文件路径 */ public function setCookieFile($cookie_file) { $this->cookie_file = $cookie_file; return $this; } // ----------------------- /** * curl方式提交 * * @param string $url 请求地址 * @param mixed $data 提交的数据 * @param string $user_agent 自定义的UA * @return mixed */ public function curlPost($url, $data, $user_agent = '') { if($user_agent == '') { $user_agent = $this->ua_string; } if (!is_array($data)) { $data = array($data); } $data = http_build_query($data); if (!function_exists("curl_init")) { die('undefined function curl_init'); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); $rs = curl_exec($ch); curl_close($ch); return $rs; } // ----------------------- /** * 套接字提交 * * @param string $url 请求地址 * @param mixed $data 提交的数据 * @param string $user_agent 自定义的UA * @param int $port 端口 * @param int $timeout 超时限制 * @return mixed */ public function socketPost($url, $data, $user_agent = '', $port = 80, $timeout = 30) { $url_info = parse_url(/blog_article/$url/index.html); $remote_server = $url_info['host']; $remote_path = $url_info['path']; $socket = fsockopen($remote_server, $port, $errno, $errstr, $timeout); if(!$socket) { die("$errstr($errno)"); } if($user_agent == '') { $user_agent = $this->ua_string; } if (!is_array($data)) { $data = array($data); } $data = http_build_query($data); fwrite($socket, "POST {$remote_path} HTTP/1.0\r\n"); fwrite($socket, "User-Agent: {$user_agent}\r\n"); fwrite($socket, "HOST: {$remote_server}\r\n"); fwrite($socket, "Content-type: application/x-www-form-urlencoded\r\n"); fwrite($socket, "Content-length: " . strlen($data) . "\r\n"); fwrite($socket, "Accept:*/*\r\n"); fwrite($socket, "\r\n"); fwrite($socket, "{$data}\r\n"); fwrite($socket, "\r\n"); $header = ""; while($str = trim(fgets($socket, 4096))) { $header .= $str; } $data = ""; while(!feof($socket)) { $data .= fgets($socket, 4096); } return $data; } // ----------------------- /** * 文件流提交 * * @param string $url 提交地址 * @param string $data 数据 * @param string $user_agent 自定义的UA * @return mixed */ public function streamPost($url, $data, $user_agent = '') { if($user_agent == '') { $user_agent = $this->ua_string; } if (!is_array($data)) { $data = array($data); } $data = http_build_query($data); $context = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded' . "\r\n" . 'User-Agent : ' . $user_agent . "\r\n" . 'Content-length: ' . strlen($data), 'content' => $data ) ); $stream_context = stream_context_create($context); $data = file_get_contents($url, FALSE, $stream_context); return $data; } // ----------------------- /** * 发送请求 * * 本方法通过curl函数向目标服务器发送请求 * * @param string $url 请求地址 * @return mixed */ public function request($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERAGENT, !empty($this->ua_string)? $this->ua_string : $_SERVER['HTTP_USER_AGENT']); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); if (isset($this->cookie_file)) { curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_file); } $data = curl_exec($ch); curl_close($ch); return $data; } } ?>
调用示例:
<?php require_once 'HttpHelper.php'; $HttpHelper = new HttpHelper(); $url = "http://localhost/post.php"; $data = array("name"=>"socket"); $rs[] = $HttpHelper->post($url, $data); $data = array("name"=>"curl"); $rs[] = $HttpHelper->post($url, $data, "curl"); $data = array("name"=>"stream"); $rs[] = $HttpHelper->post($url, $data, "stream"); $rs[] = $HttpHelper->request($url); print_r($rs); ?>
3,post.php文件:
<?php echo 'test request:'; print_r($_REQUEST); ?>
4,输出结果:
Array
(
[0] => test request:Array
(
[name] => socket
)
[1] => test request:Array
(
[name] => curl
)
[2] => test request:Array
(
[name] => stream
)
[3] => test request:Array
(
)
)