Solved

Trying to make sense of $stack[count($stack)] = &$tree;

Posted on 2008-10-20
4
216 Views
Last Modified: 2012-05-05
I'm trying to roll an XML file into a PHP array using Expat Parser, and I'm relatively new to PHP.  I came across the following code: http://us.php.net  /xml_set_element_handler.  (The sample code is about 1/3 the way down the page, author: "rubentrancoso at gmail dot com".)

I'm struggling with the last line of code in the following code block:

$file = "flow/flow.xml";
$depth = 0;
$tree = array();
$tree['name'] = "root";
$stack[count($stack)] = &$tree;

1) As I understand it, count returns the number of elements in the array, but there are no elements in the array, because $stack was just declared.  Right?
2) What's up with &$tree?  

(I have also attached the full sample of the code.)  
<?php
 

$file = "flow/flow.xml";

$depth = 0;

$tree = array();

$tree['name'] = "root";

$stack[count($stack)] = &$tree;
 

function startElement($parser, $name, $attrs) {

   global $depth;

   global $stack;

   global $tree;

  

   $element = array();

   $element['name'] = $name;

   foreach ($attrs as $key => $value) {

        //echo $key."=".$value;

        $element[$key]=$value;

    }
 

   $last = &$stack[count($stack)-1];

   $last[count($last)-1] = &$element;

   $stack[count($stack)] = &$element;
 

   $depth++;

}
 

function endElement($parser, $name) {

   global $depth;

   global $stack;
 

   array_pop($stack);

   $depth--;

}
 

$xml_parser = xml_parser_create();

xml_set_element_handler($xml_parser, "startElement", "endElement");

if (!($fp = fopen($file, "r"))) {

   die("could not open XML input");

}
 

while ($data = fread($fp, 4096)) {

   if (!xml_parse($xml_parser, $data, feof($fp))) {

       die(sprintf("XML error: %s at line %d",

                   xml_error_string(xml_get_error_code($xml_parser)),

                   xml_get_current_line_number($xml_parser)));

   }

}

xml_parser_free($xml_parser);

$tree = $stack[0][0];

echo "<pre>";

print_r($tree);

echo "</pre>";

?>

Open in new window

0
Comment
Question by:jdana
  • 2
  • 2
4 Comments
 
LVL 15

Expert Comment

by:MMDeveloper
ID: 22758179
stack[count($stack)] is the same as

$stack[] =

it simply says that the following value will be appended to the end of the array.


&$tree is a reference pointer per say. That means that whatever changes are made to the value in $stack[count($stack)] are replicated automatically to the original $tree value.


If I set this

$a = 5;
$b = &$a;

$b = 0;

now both $b and $a = zero. What happens to $b also happens to $a automatically.
0
 

Author Comment

by:jdana
ID: 22758813
Does $b = &$a; set $b equal to $a BY REFERENCE as opposed to BY VALUE?
0
 
LVL 15

Accepted Solution

by:
MMDeveloper earned 250 total points
ID: 22758924
you are correct.
0
 

Author Closing Comment

by:jdana
ID: 31507826
Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now