1

I'm attempting to print an array of php elements embedded in html

If I input

echo '<strong>'.$s[firstname].' '.$s[lastname].'</strong><div class="moreinfo"><p><small>'.$s[role].' of '.$parent'.</small></p></div></li>';

I get a result that says something like "Chris James Parent of Array

but if I attempt to print the array with a foreach as so

echo '<strong>'.$s[firstname].' '.$s[lastname].'</strong><div class="moreinfo"><p><small>'.$s[role].' of '.
            foreach($parent as $p){
                echo $p.' ';
            }
            .'</small></p></div></li>';

The program crashes completely. I would assume that I'm doing something syntactically incorrect, but I can't spot the issue. Is there a simply way to print the elements in thearray that would avoid the crash?

Thanks in advance!

4
  • Use debug($s) in your array to see how structured are. When you see white screen, probably syntax error. Commented Dec 29, 2014 at 18:44
  • Could you clarify? I didn't understand your statement Commented Dec 29, 2014 at 18:46
  • When you see "Array" printed, it means you have an array and cannot printed without index value. Try to add this in your code before the foreach statement: debug($parent); This function will show you values inside $parent variable structured. Commented Dec 29, 2014 at 18:49
  • 1
    Unless firstname and lastname are defined constants, then they should be quoted in $s['firstname'] and $s['lastname'] Commented Dec 29, 2014 at 18:50

1 Answer 1

2

You concatenate output with . not additional PHP statements:

echo '<strong>'.$s[firstname].' '.$s[lastname].'</strong><div class="moreinfo"><p><small>'.$s[role].' of ';

            foreach($parent as $p){
                echo $p.' ';
            }

echo '</small></p></div></li>';

However you can just implode $parent:

echo '<strong>'.$s[firstname].' '.$s[lastname].'</strong><div class="moreinfo"><p><small>'.$s[role].' of '.implode(' ', $parent).'.</small></p></div></li>';
Sign up to request clarification or add additional context in comments.

1 Comment

This fixed it right up. I think at this point I just need some sleep. Thanks for your help!

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.