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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
imsoconfused
Forum Newbie
Posts: 15
Joined: Mon Oct 05, 2009 10:55 pm

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

Post 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
 
jamesm6162
Forum Newbie
Posts: 13
Joined: Sat Jun 28, 2008 10:31 am

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

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
imsoconfused
Forum Newbie
Posts: 15
Joined: Mon Oct 05, 2009 10:55 pm

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

Post 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
jamesm6162
Forum Newbie
Posts: 13
Joined: Sat Jun 28, 2008 10:31 am

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

Post 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.
Post Reply