PHP array 中追加實體類的實體

來源:互聯網
上載者:User
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;}
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.