Forum Moderators: coopster

Message Too Old, No Replies

php array and $i variable

         

CodilX

10:07 am on Feb 9, 2010 (gmt 0)

10+ Year Member



this is the problem..

i have a huge array (imported from an xml file), and i'm trying to extract the info.

the problem is, that i have a for loop from 1 to 10, and i want the $i variable to be linked with the echo statement, but i can't get it to work.. in other words.. :


for($i=1;$i <= 10;$i++) {
echo $xml->content->numbered_content[b]$i[/b]->header; (of course this doesn't work, thus my problem)
}


array looks smth likes this:

content =>
(
[numbered_content[b]1[/b]] =>
(
header => bla
content => bla
)
[numbered_content[b]2[/b]] =>
(
header => bla
content => bla
)
[numbered_content[b]3[/b]] =>
(
header => bla
content => bla
)
)


i hope someone understood my problem.. i just need to know how to include a variable in that echo statement. thank you!

Little_G

10:13 am on Feb 9, 2010 (gmt 0)

10+ Year Member



Hi,

Try:
for($i=1;$i <= 10;$i++) {
echo $xml->content->{'numbered_content' . $i}->header;
}


Andrew

Matthew1980

10:18 am on Feb 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Codilx,


echo $xml->content->numbered_content$i->header; (of course this doesn't work, thus my problem)


Just reading this quickly, dont you just need to concatonate the vars?


echo $xml->content->numbered_content.$i.->header; (of course this doesn't work, thus my problem)


I'm not to sure on xml though but thats how I read the code. just thought I would make the suggestion... Then again you have more than likely tried this already...

Cheers,

MRb

CodilX

10:38 am on Feb 9, 2010 (gmt 0)

10+ Year Member



thank you so much! thank you!

Matthew1980

10:55 am on Feb 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there codilx,

I'm curious now, what was the solution, as I'm not familiar with XML, but I intend to start..

Cheers,

MRb