首页 | 源码下载 | 网站模板 | 网页特效 | 广告代码 | 网页素材 | 字体下载 | 书库 | 站长工具
会员投稿 投稿指南 RSS订阅
当前位置:主页>网络编程>PHP教程>资讯:PHP 大数组循环问题

PHP 大数组循环问题

www.jz123.cn  2013-12-25   来源:   站长家园    责任编辑(袁袁)    我要投递新闻

小妹刚刚改投PHP门下。领导叫我把这段代码的执行效率优化一下

我现在知道的优化就是小循环外面,好像在这没啥用。

请问各位大侠我该怎么优化ne ? 领导说放内存里什么的。

基本就是2个大数组不停的循环算权重。

001.<?php
002.error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT);
003.
004.class weight {
005. private $_aItems = array();
006. private $_aTable = array();
007. private $_aDict = array();
008. private $_aMatchs = array();
009. private $_aShow = array();
010. function __construct() {
011. }
012.
013. public function newItems($aItems){
014. //添加新的检索内容
015. if (!is_array($aItems))
016. $aItems = (array)$aItems;
017. $this->_aItems = $aItems;
018. $this->_aMatchs = array();
019. $this->_aShow = array();
020. }
021.
022. public function newTable($aTable){
023. if (!is_array($aTable))
024. $aTable = (array)$aTable;
025. $this->_aTable = $aTable;
026. $this->generateDict();
027. }
028.
029. private function generateDict() {
030. //将字典处理成数组形式
031. $convert = function($value) {
032. $value = str_replace('|', ',', $value);
033. $value = explode(',', $value);
034. return $value;
035. };
036. $this->_aDict = array_map($convert, $this->_aTable);
037. }
038.
039. public function getMatchs() {
040. //返回对照表
041. return $this->_aMatchs;
042. }
043.
044. public function getShow($sRule = 'debug') {
045. /*返回格式化的结果集
046. * $sFormat: 指定输出格式
047. */
048. if (emptyempty($this->_aItems)||emptyempty($this->_aTable))
049. //字典源文件不存在
050. return false;
051. if (emptyempty($this->_aShow)) {
052. /*匹配表还没有生成,自动调用相应的命令生成*/
053. $this->loopTable();
054. }
055. $makeDumpStr = function($value, $key) use (&$dumpStr) {
056. //生成导出文件的文本
057. if (count($value) >1) {
058. foreach ($value as $valueOne) {
059. $valueStr .= $valueOne. ',';
060. }
061. $dumpStr .= $this->_aItems[$key] . "\t匹配多个记录号\t". $valueStr ."\r\n";
062. } else {
063. $dumpStr .= $this->_aItems[$key] . "\t匹配惟一记录号\t". $value[0] ."\r\n";
064. }
065. };
066. switch($sRule) {
067. case 'debug':
068. print_r($this->_aShow);
069. break;
070. case 'json':
071. return json_encode($this->_aShow);
072. break;
073. case 'txt':
074. $timeExport = date("Y/M/D h:i:s");
075. $dumpStr = '';
076. $rFile = fopen('dump.txt', 'w');
077. array_walk($this->_aShow, $makeDumpStr);
078. $sContent = <<<EOT
079.========DUMP-FILE-{$timeExport}=========================
080.{$dumpStr}
081.EOT;
082. fwrite($rFile, $sContent);
083. fclose($rFile);
084. break;
085. default:
086. return $this->_aShow;
087. break;
088. }
089. }
090.
091. private function loopTable() {
092. //遍历
093. foreach ($this->_aItems as $iItemKey=> $sItemLine) {
094. $this->matchElement($iItemKey);
095. //print_r($this->_aMatchs);
096. $this->match2Show($iItemKey);
097. //print_r($this->_aShow);
098. //echo "-----------------";
099. }
100. //print_r($this->_aMatchs);
101. //print_r($this->_aShow);
102. }
103.
104. private function matchElement($iKey) {
105. $iMax = 0;
106. foreach ($this->_aDict as $iDictKey => $aDictLine) {
107. foreach($aDictLine as $sDictElement) {
108. $str = $this->_aItems[$iKey];
109. if(strstr($str, $sDictElement)){
110. //匹配到一个元素,计数器+1
111. ++$this->_aMatchs[$iKey]['keyring'][$iDictKey];
112. }
113. }
114. if (!$this->_aMatchs[$iKey]['keyring'][$iDictKey]) {
115. //没有匹配到内容
116. $this->_aMatchs[$iKey]['keyring'][$iDictKey] = 0;
117. }
118. if ($iMax< $this->_aMatchs[$iKey]['keyring'][$iDictKey])
119. $iMax = $this->_aMatchs[$iKey]['keyring'][$iDictKey];
120. $this->_aMatchs[$iKey]['index'] = array(
121. 'key' => $iDictKey,
122. 'count' => $iMax
123. );
124.
125. }
126. }
127.
128. private function match2Show($iKey) {
129. //将对照表转化为结果集
130. $multiMatch = array();
131. //echo "ikey =". $iKey.", ";
132. foreach ($this->_aMatchs[$iKey]['keyring'] as $iMatchKey => $iVal) {
133. if ($iVal< $this->_aMatchs[$iKey]['index']['count']) {
134. //这个值比最大值小
135. //echo "x";
136. continue;
137. } else {
138. //这个值跟最大值相等,将结果累加到记录中
139. //echo "y";
140. $multiMatch[] = $iMatchKey;
141. }
142. }
143. if (count($multiMatch)> 1)
144. //多于一条记录匹配值相同
145. $this->_aShow[$iKey] = $multiMatch;
146. else
147. //匹配值最大值唯一
148. $this->_aShow[$iKey] = array($this->_aMatchs[$iKey]['index']['key']);
149. }
150.}
151.
152.$aItems = array(
153. 'chinaisbig',
154. 'whichisnot',
155. .....
156. 上万条
157. .....
158. 'totalyrightforme',
159.);
160.$aTable = array(
161. 'china,is|small',
162. 'china,big|me',
163. .....
164. 上千条
165. .....
166. 'china,is|big,wich|not,me',
167.);
168.$weight = new weight();
169.$weight->newItems($aItems);
170.$weight->newTable($aTable);
171.
172.$weight->getShow('debug');
173.
174.?>

上一篇:PHP FTP操作类( 上传、拷贝、移动、删除文件/创建目录 ) 下一篇:没有了

评论总数:0 [ 查看全部 ] 网友评论


关于我们隐私版权广告服务友情链接联系我们网站地图