Append entity class entities to PHP array

Source: Internet
Author: 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)//the line exists ' = ' $val=substr ($line, $eq + 1); if (Strpos ($line, "QoS". $i. " _en ") ===0) {$qos->qos= $val;//Connection Status}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-&Gt;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);//Here the printing of each QoS is normal array_unshift ($qosarry, $qos);//print_r ($qosarry);//The value printed here Qosarry is empty. Note: $i = 3 or 4 O'Clock the value of QoS is null}print_r ($qosarry), return $qosarry, Class Qosclass{public $qos,//Enable 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 $qos 1_dl_mbr;public $qos _ul_mbr;} 

Why is the value gone after the unshift? or is the value of the fourth replaced by the previous number of values?


Reply to discussion (solution)

Urgent and urgent urgent!!!!!!!!!!!!!!!!!!!!!!! God help me!!!!!!!!!!!!!!!!!!!!!!!!!!!!

A "}" is missing from the front of class

Take a look at $file = "/usr/local/lte/em100cfg". $ethnumber; of the content

It is supposed that the value of the 4 elements of the $qosarry is the same (because the object is always passed by reference)

Take a look at $file = "/usr/local/lte/em100cfg". $ethnumber; of the content

It is supposed that the value of the 4 elements of the $qosarry is the same (because the object is always passed by reference)


I'll post the contents of the file.
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= Enable

Qos2_cid=id

QOS2_PACKET_FILTER_IDENTIFIER=TFT Marking

Qos2_evaluation_precedence_index=tft Priority level

qos2_source_address= in situ value

qos2_subnet_mask=

Qos2_protocol_number= protocol Number

Qos2_destination_port_range= Destination Port Range

Qos2_source_port_range= Remote port range

qos2_direction= direction

Qos2_qci=qci

Qos2_dl_gbr= downlink GBR

Qos2_ul_gbr= Upstream

Qos2_dl_mbr=

Qos2_ul_mbr=

Qos2_cfg_result=1&0 1 = Success



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=

I tried to loop to 2, which is the value of two. This time Qosarry is normal, but it's wrong to add 3.

That's right, isn't it?
Gos3_xxxx and gos4_xxxx are empty in the data.
So the $qosarry is empty.

Put Array_unshift ($qosarry, $qos);
Change to Array_unshift ($qosarry, clone $qos);
It's right.

That's right, isn't it?
Gos3_xxxx and gos4_xxxx are empty in the data.
So the $qosarry is empty.

Put Array_unshift ($qosarry, $qos);
Change to Array_unshift ($qosarry, clone $qos);
It's right.


Oh, yes, thank you, big-god rookie. Thank you, great God.

Actually, it's better if you write the function.

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);p Reg_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;}
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.