View Full Version : SimpleXML Array Fucntions
bluewalrus
04-25-2011, 02:58 AM
How can I use array functions on a simplexml object? For example
<?php
$xml = simplexml_load_file('log.xml');
$ua = $_SERVER['HTTP_USER_AGENT'];
$ip = $_SERVER['REMOTE_ADDR'];
foreach ($xml as $visitor) {
array_search($ip, $visitor);
}
?>
<?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.
bluewalrus
04-25-2011, 01:27 PM
Aha, returning to my code was a better idea, for anyone that stumbles on this
<?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.
$visitor->addChild('ip',$_SERVER['REMOTE_ADDR']); :D
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.
bluewalrus
04-25-2011, 03:59 PM
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
$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
<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.
//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.
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.
$xml->asXML('log.xml')
BTW when you do this:
$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):
$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');
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.