Home  >  Article  >  Backend Development  >  PHP array 中追加实体类的实体

PHP array 中追加实体类的实体

WBOY
WBOYOriginal
2016-06-23 13:50:30772browse

function getqos_one($ethnumber)	{		$qos = new qosclass();		$file="/usr/local/lte/em100cfg".$ethnumber;		echo $file;		$content=file_get_contents($file);		$list=explode("\n",$content);		$qosarry=array();		for($i=1;$i<5;$i++)		{		foreach($list as $line)		{			$line=trim($line);			$eq=strpos($line,'=');			if($eq>0)//该行存在‘=’号				$val=substr($line,$eq+1);						if(strpos($line,"qos".$i."_en")===0)			{				$qos->qos=$val;//连接状态			}			else if(strpos($line,"qos".$i."_cid")===0)			{				$qos->qos_cid=$val;			}			else if(strpos($line,"qos".$i."_packet_filter_identifier")===0)			{				$qos->qos_packet_filter_identifier=$val;			}			else if(strpos($line,"qos".$i."_evaluation_precedence_index")===0)			{				$qos->qos_evaluation_precedence_index=$val;			}			else if(strpos($line,"qos".$i."_source_address")===0)			{				$qos->qos_source_address=$val;			}			else if(strpos($line,"qos".$i."_subnet_mask")===0)			{				$qos->qos_subnet_mask=$val;			}			else if(strpos($line,"qos".$i."_protocol_number")===0)			{				$qos->qos_protocol_number=$val;			}			else if(strpos($line,"qos".$i."_destination_port_range")===0)			{				$qos->qos_destination_port_range=$val;			}			else if(strpos($line,"qos".$i."_source_port_range")===0)			{				$qos->qos_source_port_range=$val;			}			else if(strpos($line,"qos".$i."_direction")===0)			{				$qos->qos_direction=$val;			}			else if(strpos($line,"qos".$i."_QCI")===0)			{				$qos->qos_QCI=$val;			}			else if(strpos($line,"qos".$i."_DL_GBR")===0)			{				$qos->qos_DL_GBR=$val;			}			else if(strpos($line,"qos".$i."_UL_GBR")===0)			{				$qos->qos_UL_GBR=$val;			}			else if(strpos($line,"qos".$i."_DL_MBR")===0)			{				$qos->qos_DL_MBR=$val;			}			else if(strpos($line,"qos".$i."_UL_MBR")===0)			{				$qos->qos_UL_MBR=$val;			}					}		//print_r($qos);//这里打印每一个qos都是正常的		array_unshift($qosarry,$qos);		//print_r($qosarry);//这里打印qosarry里面的值 就全是空的了 注:$i=3或者4时 qos的值都是空		}		print_r($qosarry);		return $qosarry;class qosclass	{		public $qos;//使能		public $qos_cid;		public $qos_packet_filter_identifier;		public $qos_evaluation_precedence_index;		public $qos_source_address;		public $qos_subnet_mask;		public $qos_protocol_number;		public $qos_destination_port_range;		public $qos_source_port_range;		public $qos_direction;		public $qos_QCI;		public $qos_DL_GBR;		public $qos_UL_GBR;		public $qos1_DL_MBR;		public $qos_UL_MBR;	}

为何在unshift之后 值就没了呢?还是第四个的值取代了前面几个的值?


回复讨论(解决方案)

急急急急急!!!!!!!!!!!!!!!!!!!!!!!大神们 帮帮忙啊 !!!!!!!!!!!!!!!!!!!!!!!!!!!!

class前面少了个"  }  " 

看一下 $file="/usr/local/lte/em100cfg".$ethnumber; 的内容

按理说 $qosarry 的 4 个元素的值都是一样的才对(因为对象总是以引用传递的)

看一下 $file="/usr/local/lte/em100cfg".$ethnumber; 的内容

按理说 $qosarry 的 4 个元素的值都是一样的才对(因为对象总是以引用传递的)


文件里面的内容我贴上来吧
qos1_en=yes

qos1_cid=2

qos1_packet_filter_identifier=1

qos1_evaluation_precedence_index=1

qos1_source_address=192.168.1.1

qos1_subnet_mask=255.255.255.255

qos1_protocol_number=

qos1_destination_port_range=

qos1_source_port_range=

qos1_direction=3

qos1_QCI=4

qos1_DL_GBR=1024

qos1_UL_GBR=1024

qos1_DL_MBR=2048

qos1_UL_MBR=2048

qos1_cfg_result=



qos2_en=使能

qos2_cid=id

qos2_packet_filter_identifier=tft标号

qos2_evaluation_precedence_index=tft优先级

qos2_source_address=原地值

qos2_subnet_mask=

qos2_protocol_number=协议号

qos2_destination_port_range=目的端口范围

qos2_source_port_range=远端口范围

qos2_direction=方向

qos2_QCI=qci

qos2_DL_GBR=下行GBR

qos2_UL_GBR=上行

qos2_DL_MBR=

qos2_UL_MBR=

qos2_cfg_result=1&0 1=成功



qos3_en=

qos3_cid=

qos3_packet_filter_identifier=

qos3_evaluation_precedence_index=

qos3_source_address=

qos3_subnet_mask=

qos3_protocol_number=

qos3_destination_port_range=

qos3_source_port_range=

qos3_direction=

qos3_QCI=

qos3_DL_GBR=

qos3_UL_GBR=

qos3_DL_MBR=

qos3_UL_MBR=

qos3_cfg_result=



qos4_en=

qos4_cid=

qos4_packet_filter_identifier=

qos4_evaluation_precedence_index=

qos4_source_address=

qos4_subnet_mask=

qos4_protocol_number=

qos4_destination_port_range=

qos4_source_port_range=

qos4_direction=

qos4_QCI=

qos4_DL_GBR=

qos4_UL_GBR=

qos4_DL_MBR=

qos4_UL_MBR=

qos4_cfg_result=

我试过只循环到2   就是有值的两个  这时候qosarry是正常的 但是只要把 3 加进去就错了 

这不就对了吗?
数据中 gos3_xxxx、gos4_xxxx 都是空的
所以 $qosarry 中也就空了

把 array_unshift($qosarry,$qos);
改成 array_unshift($qosarry, clone $qos);
就正确了

这不就对了吗?
数据中 gos3_xxxx、gos4_xxxx 都是空的
所以 $qosarry 中也就空了

把 array_unshift($qosarry,$qos);
改成 array_unshift($qosarry, clone $qos);
就正确了


对了 谢谢大神 菜鸟不会啊 刚学 谢谢了 大神 

其实你把函数写成这样比较好

function getqos_one($ethnumber){	$file="/usr/local/lte/em100cfg".$ethnumber;	$list = file($file, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);	$qosarry=array();	foreach($list as $r) {		list($key, $value) = explode('=', $r);		preg_match('/(\D+)(\d+)(.+)/', $key, $m);		$key = $m[1] . ($m[3] != '_en' ? $m[3] : '');		$i = $m[2];		if(! isset($gosarray[$i])) $gosarray[$i] = new qosclass();		$gosarray[$i]->$key = $value;	}	return $gosarray;}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn