/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] accessing sequentially named elements of an object

My experience with objects is so limited that I probably don’t use the right words for things, but I’m learning.

I get data from an API as XML and use simplexml_load_string to parse it into an object.

I have elements like this:

[CODE]$results->Detail->Price1
$results->Detail->Price2
$results->Detail->Price3
$results->Detail->Price4
$results->Detail->Price5[/CODE]

How do I work with these sequentially?

Similar to how I’d do this:

[CODE]
$var1=”foo”;
$var2=”bar”;
//…
$i=1;
while ($i < 3){ //or <= number of vars
$nowvar=’var’.$i;
echo ($$nowvar.”n”);
$i++;
}
//foo
//bar
[/CODE]

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogDec 16.2010 — I think you could do something like:
[code=php]
echo $result->Detail->{"Price$i"};
// ...or maybe just...
echo $result->Detail->{Price$i};
[/code]

If that does not work, you could try it in 2 steps:
[code=php]
$item = "Price" . $i;
echo $result->Detail->$item;
[/code]
Copy linkTweet thisAlerts:
@TecBratauthorDec 16.2010 — Thanks.

Your suggestion worked:

[code=php]$col=1;
while ($col <=6){
$col_price=$results->Detail->{"Price$col"};
echo('<!-- Column '.$col.' is '.$col_price.' -->'."n");
$col++;
}[/code]


Then I tweaked it to
[code=php]
//...
$col_price=$results->Detail->{Price.$col};
//...[/code]

And it still worked. I liked the style better.
Copy linkTweet thisAlerts:
@NogDogDec 16.2010 — ...

Then I tweaked it to
[code=php]
//...
$col_price=$results->Detail->{Price.$col};
//...[/code]

And it still worked. I liked the style better.[/QUOTE]


Note that that would generate a notice-level warning about "Price" being an undefined constant. To avoid that, you would need to make it a string literal by quoting it:
[code=php]
$results->Detail->{'Price'.$col};
[/code]
Copy linkTweet thisAlerts:
@TecBratauthorDec 16.2010 — You're right, and that's actually the style I was looking for, but I think I tried it without the curly braces, so it failed.

I'll use that style.

Thanks again.
Copy linkTweet thisAlerts:
@TecBratauthorDec 16.2010 — NogDog,

this:[code=php]$col=$results->Detail->{'Price'.$col};
var_dump($col);
[/code]


shows something like
[CODE]object(SimpleXMLElement)#3 (1) {
[0]=>
string(4) "0.11"
}[/CODE]

I wanted it to just be a string, "0.11"

How do I get that? (thinking something like array_values)
Copy linkTweet thisAlerts:
@NogDogDec 16.2010 — Looks like this works:
[code=php]
$col=$results->Detail->{'Price'.$col}[0];
[/code]
Copy linkTweet thisAlerts:
@TecBratauthorDec 16.2010 — Looks like this works:
[code=php]
$col=$results->Detail->{'Price'.$col}[0];
[/code]
[/QUOTE]


I tried that, and $col is still a copy of the property, not just the value of it.

This worked though:[code=php]$col=(string)$results->Detail->{'Price'.$col}[0];[/code]
×

Success!

Help @TecBrat 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.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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