php 代码优化之经典示例
作者:bea
我用的方法是按key区分块,然后在将块赋给其他的变量,然后再进行一些操作,这样用到了很多的for和foreach,而且代码量也很大,所以被退回来了。 经过上面的指导,发现真的好简单,现在与大家一同分享。 ID FIELD1 FIELD2 FIELD3 FIELD4 Key 1 *** *** *** *** meat1 2 *** *** ***
我用的方法是按key区分块,然后在将块赋给其他的变量,然后再进行一些操作,这样用到了很多的for和foreach,而且代码量也很大,所以被退回来了。
经过上面的指导,发现真的好简单,现在与大家一同分享。
ID
FIELD1
FIELD2
FIELD3
FIELD4
Key
1
***
***
***
***
meat1
2
***
***
***
***
meat1
3
***
***
***
***
meat1
4
***
***
***
***
meat1
5
***
***
***
***
fruit2
6
***
***
***
***
fruit2
7
***
***
***
***
fruit2
8
***
***
***
***
fruit2
9
***
***
***
***
fruit2
10
***
***
***
***
food3
11
***
***
***
***
food3
现在有如上所示的结果
要求:要对这个已经按key进行排序了的数组进行操作,相同key的项进行处理。
提示:这个是很典型的母子表的结构,也就是说其实它是两张表的合并,可以这样处理成两个数组,方便数组里面对块的操作array1:ID|Key
ID
Key
1
meat1
2
meat1
3
meat1
4
meat1
5
fruit2
6
fruit2
7
fruit2
8
fruit2
9
fruit2
10
food3
11
food3
array2:key => array(ID,FIELD1,FIELD2,FIELD3,FIELD4,FIELD5,Key)
ID
FIELD1
FIELD2
FIELD3
FIELD4
Key
meat1=>
1
***
***
***
***
meat1
2
***
***
***
***
meat1
3
***
***
***
***
meat1
4
***
***
***
***
meat1
fruit2=>
5
***
***
***
***
fruit2
6
***
***
***
***
fruit2
7
***
***
***
***
fruit2
8
***
***
***
***
fruit2
9
***
***
***
***
fruit2
food3=>
10
***
***
***
***
food3
11
***
***
***
***
food3
实现如上数组分离代码
这样后,访问tempArray的块数据就非常方便了
foreach($tempArray as $row){
array1[$row['ID']] = $row['Key'];
array2[$row['Key']][] = $row;
}
访问和处理代码
foreach($array1 as $ID => $Key){
$this->doSomeThing($ID);
//访问tempArray的块数组$array2[$Key]
$this->doSomeThing2($array2[$Key]);
}
if ($ != jQuery) {
$ = jQuery.noConflict();
}
var isLogined = false;
var cb_blogId = 72706;
var cb_entryId = 1994279;
var cb_blogApp = "zcy_soft";
var cb_blogUserGuid = "c4d4393c-4c87-df11-ba8f-001cf0cd104b";
var cb_entryCreatedDate = '2011/3/24 20:21:00';
有用 | 无用
经过上面的指导,发现真的好简单,现在与大家一同分享。
ID
FIELD1
FIELD2
FIELD3
FIELD4
Key
1
***
***
***
***
meat1
2
***
***
***
***
meat1
3
***
***
***
***
meat1
4
***
***
***
***
meat1
5
***
***
***
***
fruit2
6
***
***
***
***
fruit2
7
***
***
***
***
fruit2
8
***
***
***
***
fruit2
9
***
***
***
***
fruit2
10
***
***
***
***
food3
11
***
***
***
***
food3
现在有如上所示的结果
要求:要对这个已经按key进行排序了的数组进行操作,相同key的项进行处理。
提示:这个是很典型的母子表的结构,也就是说其实它是两张表的合并,可以这样处理成两个数组,方便数组里面对块的操作array1:ID|Key
ID
Key
1
meat1
2
meat1
3
meat1
4
meat1
5
fruit2
6
fruit2
7
fruit2
8
fruit2
9
fruit2
10
food3
11
food3
array2:key => array(ID,FIELD1,FIELD2,FIELD3,FIELD4,FIELD5,Key)
ID
FIELD1
FIELD2
FIELD3
FIELD4
Key
meat1=>
1
***
***
***
***
meat1
2
***
***
***
***
meat1
3
***
***
***
***
meat1
4
***
***
***
***
meat1
fruit2=>
5
***
***
***
***
fruit2
6
***
***
***
***
fruit2
7
***
***
***
***
fruit2
8
***
***
***
***
fruit2
9
***
***
***
***
fruit2
food3=>
10
***
***
***
***
food3
11
***
***
***
***
food3
实现如上数组分离代码
这样后,访问tempArray的块数据就非常方便了
foreach($tempArray as $row){
array1[$row['ID']] = $row['Key'];
array2[$row['Key']][] = $row;
}
访问和处理代码
foreach($array1 as $ID => $Key){
$this->doSomeThing($ID);
//访问tempArray的块数组$array2[$Key]
$this->doSomeThing2($array2[$Key]);
}
if ($ != jQuery) {
$ = jQuery.noConflict();
}
var isLogined = false;
var cb_blogId = 72706;
var cb_entryId = 1994279;
var cb_blogApp = "zcy_soft";
var cb_blogUserGuid = "c4d4393c-4c87-df11-ba8f-001cf0cd104b";
var cb_entryCreatedDate = '2011/3/24 20:21:00';
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- php 求质素(素数) 的实现代码
- php 5.3.5安装memcache注意事项小结
- php处理json时中文问题的解决方法
- php 面向对象的一个例子
- 深入理解PHP原理之Session Gc的一个小概率Notice
- php表单提交问题的解决方法
- 使用NetBeans + Xdebug调试PHP程序的方法
- php产生随机数的两种方法实例代码 输出随机IP
- PHP随机数生成代码与使用实例分析
- PHP的cURL库功能简介 抓取网页、POST数据及其他
- php curl 登录163邮箱并抓取邮箱好友列表的代码(经测试)
- The specified CGI application misbehaved by not returning a complete set of HTTP headers
- PHP stream_context_create()作用和用法分析
- php中突破基于HTTP_REFERER的防盗链措施(stream_context_create)
- PHP 5.3 下载时 VC9、VC6、Thread Safe、Non Thread Safe的区别分析
- php中关于普通表单多文件上传的处理方法
- PHP删除特定数组内容并且重建数组索引的方法.
- PHP学习 变量使用总结
- 优化php效率,提高php性能的一些方法