0

I have a xml file that i want to echo to the browser

echo $rule = $info->rule1

echo $rule = $info->rule2

Result:

example 1

example 2

Because the rule in the xml is dynamic i want to count how much rules there are and pass that variable behind the "rule"

$xml = simplexml_load_file('file.xml');
$xml_nested = $xml->monthlegenda;
echo "<ul>";
foreach ($xml_nested as $info):
    for($i=1; $i < count($info); $i++){
    $rule = $info->rule;
    $rule .= $i;
    echo $rule;
    };
endforeach;
echo "</ul>";

As a result i expect:

example 1

example 2

but i get 12

2
  • Please post your XML also. Commented Sep 18, 2013 at 7:59
  • I think your parsing is wrong show your XML Commented Sep 18, 2013 at 8:02

4 Answers 4

3

You're probably looking for a way to get the property of an object from a string, which is done like so:

$instance->{$var.'string-part'.$otherVar};

In your case, that'd be:

echo $info->{'rule'.$i};

For more details, refer to the man pages on variable variables. Especially the section entitled " #1 Variable property" should be of interest to you...
But since you're echoing <ul> tags before and after the loop, I'm assuming you're trying to create a list, in which case, your echo statement should look like this:

echo '<li>', $info->{'rule'.$i}, '</li>';//comma's not dots!

Note that you'll never loop through the entire $info object, because of your for loop. You should either write:

for ($i=1,$j = count($info);$i<=$j;$i++)
{
    echo $info->{'rule'.$i};
}

Note that I'm assigning count($info) to a variable, to avoid calling count on each iteration of the loop. You're not changing the object, so the count value will be constant anyway... or simply use foreach:

foreach($info as $property => $val)
{//val is probably still an object, so use this:
    echo $info->{$property};
}

In the last case, you could omit the curlies around $property, but that's not recommended, but what you can do here, is check if the property concerned is a rule property:

foreach($info as $property => $val)
{
    if (substr($property, 0, 4) === 'rule')
    {//optionally strtolower(substr($property, 0, 4))
        echo '<li>', $info->{$property}, '<li>';
    }
}

That's the easiest way of doing what you're doing, given the information you've provided...

Sign up to request clarification or add additional context in comments.

2 Comments

The first solution did show every entry, i tried the foreach but is it nested in the for loop because it does not check all rule's
@MichaelAngelo: setup a pastebin, or add the foreach code to your question, so I can see what you're doing, but using the foreach, you don't need the for loop any longer. The foreach replaces the for loop, but it's still "nested" inside the foreach($xml_nested as $info) loop
2

Try this code for "for":

for($i=1; $i < count($info); $i++)
{
    $rule = $info->{'rule'.$i};
    echo $rule;
}

3 Comments

There's an issue with this code: you'll not loop through all properties of $info (the last one will be skipped), and you're calling count on each iteration. Check my answer for details...
This works thanks!, but how can i make sure i get all the "rule" because i count all childeren i have to "remove the count" for the children that are not "rule"
@MichaelAngelo: Added the answer to that to the bottom of my answer (it's by using a foreach loop: objects can be iterated over using foreach)
1

Try this one:

$rule = $info->{rule.$i};

Comments

0

Check this

$xml = simplexml_load_file('file.xml');
$xml_nested = $xml->monthlegenda;
echo "<ul>";
foreach ($xml_nested as $info) 
    for($i=1; $i < count($info); $i++){
    $rule = "";
    $rule = $info->rule;
    $rules =  $rule.$i;
    echo $rules;
    } 
echo "</ul>";

1 Comment

No, that won't make a difference since $rule is overwritten with $info->rule; in the next statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.