Variable reference in PHP

Source: Internet
Author: User

$ Xml = '.. /data/news. xml'; <br/> $ QP = QP ($ XML, 'news _ root')-> Children (); </P> <p> $ arrdata = array (); <br/> $ I = 0; <br/> foreach ($ QP as $ child) <br/>{< br/> print_r ($ arrdata); <br/> echo "<br/> Hi <br/> "; <br/> unset ($ arrsub); <br/> $ arrsub = array (); <br/> array_push ($ arrdata, & $ arrsub ); <br/> print_r ($ arrdata); <br/> echo "<br/>"; <br/> // $ arrdata [] = & $ arrsub; <br/> $ child = $ child-> Children (); <br/> foreach ($ child as $ item) <br/>{< br/> $ arrsub [$ item-> tag ()] = $ item-> text (); <br/>}< br/> print_r ($ arrdata); <br/> echo "<br/>"; <br/>}

The aboveCodeNote the unset ($ arrsub) statement.

My intention is $ arrdata to parse the XML Layer 2 content.

I use array_push to press a referenced array arrsub,

 

Then, the arrsub operation is equivalent to modifying arrdata.

However, this is actually a problem.

It shows that the last content of arrdata is the copy of the last XML content. (My XML is empty at last, so arrdata is: [[], [], rather than correct: [[data1, data2], []

After I press arrsub into arrdata by reference,

After a loop, the statement $ arrsub = array () is encountered. After this statement is executed, one of arrdata becomes an empty array.

 

Therefore, unset ($ arrsub) must be removed to solve the problem.

 

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.