Page 1 of 1

print_r works but echo doesn't..convert object to string?

Posted: Mon Dec 21, 2009 1:14 pm
by imsoconfused
Hello everyone

I am trying to use echo to access the elements in an array but nothing happens.. When I use Print_r i get SimpleXMLElement Object() for each item that is supposed to be in the list.

This is what I have done

1. Load XML file with SimpleXML
2. created a function that puts the XML items into an array
3. created a function to errase the contents of the XML FILE
4. created a function to put the items in the array back into the xml file

step 1 and 2 are correct and allow me to use echo to retrieve the items in the array and display them
but when i perform step 3 and erase the xml file the echo no longer works....but the print_r will work by giving me SimpleXMLElement Object() in the display for every item instead of the actual item ccontent. what am i doing wrong?? Sorry if it is easy i am still very new to PHP . I will post the code ...it is important to get it working because i plan to make modify the array with php before reconstructing the xml file

Code: Select all

 
<?php
 
$xml = simplexml_load_file('photoFile.xml');      //loads the xml file
 
$blackhole = array();    //Array where all XML elements will be stored
 
//STEP 1//////////////////////////////////////
function xmlToArray($xml, &$blackhole)
{
  $saveMe = $xml;
  $i = 0;
    foreach($saveMe->item as $item)
    {
    $blackhole[$i] = $item->location;          //add string to current array index
    $i++; //add 1 to the array number
    }   
}
 
// STEP 2 ////////////////////////////////////////
function erraseXML($xml)
{
    foreach($xml->item as $item)
    {
    unset($xml->item); //Erases all top elements named item 
    }
    echo "ERASING IS DONE";
}
 
 
 
///////////////DISPLAY ELEMENTS IN THE ARRAY NOT WORKING WITH ECHO
$counta = count($blackhole);
    
    for($i=0; $i<$counta; $i++)
    {
    echo $blackhole[$i];
    echo "<br />";
        }
 
$xml->asXML('photoFile.xml');   //save the xml changes
 

Re: print_r works but echo doesn't..convert object to string?

Posted: Mon Dec 21, 2009 1:45 pm
by jamesm6162
I am not sure I understand your problem exactly.

I will need to see the exact sequence of code, because the two functions are not called anywhere.
Also please indicate exactly which lines do not execute as expected?
unset($xml->item); //Erases all top elements named item
You probably meant:

Code: Select all

unset($item);
which wouldn't work anyway (I think) because PHP passes these items by value (copies) and not by reference, so the unset probably wouldn't have any effect.

Re: print_r works but echo doesn't..convert object to string?

Posted: Mon Dec 21, 2009 2:08 pm
by AbraCadaver
As jamesm6162 says, it's hard to tell without all of the code, but does this help?

Code: Select all

foreach($saveMe->item as $item)
{
    $blackhole[] = (string)$item->location;          //add string to current array index
}

Re: print_r works but echo doesn't..convert object to string?

Posted: Mon Dec 21, 2009 4:20 pm
by imsoconfused
Thanks abra Cadaber and james! I had a feeling it would be something easy... i am still learning the ropes the (string) works nicely.
i call both functions in order and then i just use the code starting with counta inorder to verify the contents in the array.. finally i save the xml changes with $xml->as XML code

james -> The unset(xml->item) does work. It erases the top level element in my xml called item

Re: print_r works but echo doesn't..convert object to string?

Posted: Tue Dec 22, 2009 3:17 am
by jamesm6162
imsoconfused wrote:james -> The unset(xml->item) does work. It erases the top level element in my xml called item
Granted, but then the entire foreach is redundant as you aren't using the control variable ($item), but you are calling unset($xml->item) several times on the exact same object/set of objects.