/    Sign up×
Community /Pin to ProfileBookmark

(min(), max()) + xpath() = nonsense

Okay, I am using an XML “database” with my PHP (due to having mirror sites), and my xpath() function is not playing nice with min() and max(). The relevant XML is:

[code=html]
<novel>
<chapters>
<chap num=”1″ />
<chap num=”2″ />
<chap num=”3″ />
<chap num=”4″ />
<chap num=”5″ />
<chap num=”6″ />
<chap num=”7″ />
<chap num=”8″ />
<chap num=”9″ />
</chapters>
</novel>
[/code]

The PHP in question is:

[code=php]$chapnums = $novel->xpath(‘./child::chapters/child::chap/attribute::num’);

$first=min($chapnums);
$last=max($chapnums);
[/code]

For some reason, I’m getting the last node with min() and the first node with max() (I’ve discovered this via rearranging the elements), but as it stands, $first ends up being “9” and $last is “1”, the opposite of what I want.

What’s going on?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogDec 22.2014 — Might be informative to do this, so we know what's actually being worked with:
[code=php]
$chapnums = $novel->xpath('./child::chapters/child::chap/attribute::num');
echo "<pre>".var_export($chapnums, 1)."</pre>";
[/code]
Copy linkTweet thisAlerts:
@Mr_Initial_ManauthorDec 22.2014 — I get the following:

[code=php]array (
0 =>
SimpleXMLElement::__set_state(array(
'@attributes' =>
array (
'num' => '1',
),
)),
1 =>
SimpleXMLElement::__set_state(array(
'@attributes' =>
array (
'num' => '2',
),
)),
2 =>
SimpleXMLElement::__set_state(array(
'@attributes' =>
array (
'num' => '3',
),
)),
3 =>
SimpleXMLElement::__set_state(array(
'@attributes' =>
array (
'num' => '4',
),
)),
4 =>
SimpleXMLElement::__set_state(array(
'@attributes' =>
array (
'num' => '5',
),
)),
5 =>
SimpleXMLElement::__set_state(array(
'@attributes' =>
array (
'num' => '6',
),
)),
6 =>
SimpleXMLElement::__set_state(array(
'@attributes' =>
array (
'num' => '7',
),
)),
7 =>
SimpleXMLElement::__set_state(array(
'@attributes' =>
array (
'num' => '8',
),
)),
8 =>
SimpleXMLElement::__set_state(array(
'@attributes' =>
array (
'num' => '9',
),
)),
)[/code]
Copy linkTweet thisAlerts:
@NogDogDec 22.2014 — Ah, so you're attempting to get the min()/max() on an array of objects, not an array of numeric values. Therefore, you'd need to loop through that array to extract the 'num' value from each into a separate 1-D array, which you could then run through min() or max(). Not 100% sure what the syntax would be, but something like this (maybe?):
[code=php]
$chapNumsOnly = array();
foreach($chapnums as $obj) {
// not sure which (if either of these is right:
$chapNumsOnly[] = $obj['num'];
// or...
$chapNumsOnly[] = $obj->num;
}
$max = max($chapNumsOnly);
[/code]

Sorry I can't be more exact, as I avoid XML whenever possible. ?
Copy linkTweet thisAlerts:
@Mr_Initial_ManauthorDec 22.2014 — Yeah, looking at the output of var_export() showed me what was going on. And it would be $obj->attributes()->num;
Copy linkTweet thisAlerts:
@Mr_Initial_ManauthorDec 23.2014 — Question, NogDog: have you ever gone to write a function--and discovered you already had written one like it?
Copy linkTweet thisAlerts:
@NogDogDec 23.2014 — Question, NogDog: have you ever gone to write a function--and discovered you already had written one like it?[/QUOTE]

If you do this kind of stuff long enough, everything becomes a case a [I]déjà vu[/I]. ?
×

Success!

Help @Mr_Initial_Man spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...