Results 1 to 5 of 5

Thread: SimpleXML Array Fucntions

  1. #1
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default SimpleXML Array Fucntions

    How can I use array functions on a simplexml object? For example

    PHP Code:
    <?php
    $xml 
    simplexml_load_file('log.xml');
    $ua $_SERVER['HTTP_USER_AGENT'];
    $ip $_SERVER['REMOTE_ADDR'];
    foreach (
    $xml as $visitor) {
        
    array_search($ip$visitor);
    }
    ?>
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <visitors>
    	<visitor>
    		<date>04/24/2011</date>
    		<user_agent>Firefox 3.6.13</user_agent>
    		<ip>255.255.255.254</ip>
    		<visits>1</visits>
    	</visitor>
    	<visitor>
    		<date>04/24/2011</date>
    		<user_agent>Firefox 3.6.16</user_agent>
    		<ip>255.255.255.255</ip>
    		<visits>4</visits>
    	</visitor>
    </visitors>
    This produces
    Warning: array_search() [function.array-search]: Wrong datatype for second argument in /home/public_html/index.php on line 6
    Thanks.
    Corrections to my coding/thoughts welcome.

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Aha, returning to my code was a better idea, for anyone that stumbles on this

    PHP Code:
    <?php
    $xml 
    simplexml_load_file('log.xml');
    $ua $_SERVER['HTTP_USER_AGENT'];
    $ip $_SERVER['REMOTE_ADDR'];
    foreach (
    $xml as $visitor) {
        if(
    $visitor->ip == $ip) {
            echo 
    "Match";
        } else
            echo 
    "No Match";
    }
    ?>
    Now to figure out how to append and create.
    Corrections to my coding/thoughts welcome.

  3. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    PHP Code:
    $visitor->addChild('ip',$_SERVER['REMOTE_ADDR']); 

    BTW, there are (user-created) functions available that convert objects into arrays. For the most part, though, it's far easier (and less confusing in the long run) to just deal with them as the objects they are.

  4. The Following User Says Thank You to traq For This Useful Post:

    bluewalrus (04-25-2011)

  5. #4
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Thanks, this doesn't appear to be adding to my file though. Is there something else I need to add for it to trigger the update?

    I have this now

    PHP Code:
    <?php
    $xml 
    simplexml_load_file('log.xml');
    $ua $_SERVER['HTTP_USER_AGENT'];
    $ip $_SERVER['REMOTE_ADDR'];
    $known = array('msie''firefox''safari''webkit''opera''netscape''konqueror''gecko');
    $agent strtolower($agent $agent $_SERVER['HTTP_USER_AGENT']);
    $date date('m/d/Y H:i:s');
    if (
    preg_match('/linux/'$agent)) { 
        
    $os "Linux";
    } elseif (
    preg_match('/macintosh|mac os x/'$agent)) {
        
    $os 'Mac';
    } elseif (
    preg_match('/windows|win32/'$agent)) {
        
    $os 'Windows';
    } else {
        
    $os 'Unknown';
    }
    $pattern '#(?<browser>' join('|'$known) . ')[/ ]+(?<version>[0-9]+(?:\.[0-9]+)?)#';
    if (!
    preg_match_all($pattern$agent$matches)) return array();
    $i count($matches['browser'])-1;
    $browser ucwords($matches['browser'][$i]);
    $version $matches['version'][$i];
    foreach (
    $xml as $visitor) {
        if(
    $visitor->ip == $ip) {
            
    // Returning Visitor
            
    $visitor->visits $visitor->visits 1;
        } else {
            
    //echo "New Visitor";
            
    $visitor->addChild('ip'$ip);
        }
    }
    ?>
    I want it to add

    Code:
    <visitor>
    		<date>$date</date>
    		<user_agent>$browser $version</user_agent>
    		<os>$os</os>
    		<ip>$ip</ip>
    		<visits>0</visits>
    	</visitor>
    for each new user and update the visits for each returning visitor.

    Tried modifying it to this but still no luck.
    Code:
    		//echo "New Visitor";
    		$new_visitor = $visitor->addChild('visitor');
    		$new_vis_dets = $new_visitor->addChild('date', $date);
    		$new_vis_dets = $new_visitor->addChild('user_agent', $browser . ':' . $version);
    		$new_vis_dets = $new_visitor->addChild('os', $os);
    		$new_vis_dets = $new_visitor->addChild('ip', $ip);
    		$new_vis_dets = $new_visitor->addChild('visits', '0');
    I also know i'm in the loop because I do get the new visitor when uncommented.
    Last edited by bluewalrus; 04-25-2011 at 06:22 PM.
    Corrections to my coding/thoughts welcome.

  6. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    that is how you do it.

    to get the new structure to the file (I assume you mean log.xml) you need to save the modified xml tree.
    PHP Code:
    $xml->asXML('log.xml'
    BTW when you do this:
    PHP Code:
    $new_visitor $visitor->addChild('visitor');
            
    $new_vis_dets $new_visitor->addChild('date'$date);
            
    $new_vis_dets $new_visitor->addChild('user_agent'$browser ':' $version);
            
    $new_vis_dets $new_visitor->addChild('os'$os);
            
    $new_vis_dets $new_visitor->addChild('ip'$ip);
            
    $new_vis_dets $new_visitor->addChild('visits''0'); 
    you should be getting the data structure you want, but the $new_vis_dets variable is being overwritten each time, so in the end it will only hold visits=>0. if you need to use the info later on, that could be a problem... but if you don't need it, then you don't really need to assign the variable at all (it's fine to do just this):
    PHP Code:
    $new_visitor $visitor->addChild('visitor');
    $new_visitor->addChild('date'$date);
    $new_visitor->addChild('user_agent'$browser ':' $version);
    $new_visitor->addChild('os'$os);
    $new_visitor->addChild('ip'$ip);
    $new_visitor->addChild('visits''0'); 
    Last edited by traq; 04-26-2011 at 02:14 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •