微信号:phpdevelopers

介绍:PHP开发者(phpdevelopers)-做最专业的PHP资源分享平台!

与PHP大数组循环相关问题

2015-04-02 12:49 php开发者

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开发者 更多文章 天使湾创投Demo Day:看看“南方系”创业者在做什么? 微软日前正式宣布Windows Phone 8.1移动操作系统将于今年6月24日正式推出 小米论坛800万用户数据库泄漏 网盘中已经可下载 阿里、百度、360合作:不只是爱情这么简单 15个国外优秀的PHP在线学习网站
猜您喜欢 如何组建一支高效的测试团队 【全民抓虫】测试抓虫两不误,提交BUG奖励多! CDH大数据平台搭建 触控Intel强强联手:多维提升游戏体验 Anaconda:最佳的Python个人应用解决方案