Strange output (echo vs $str .=)

I’m having some issues with producing the right output with ($str .=) for some reason, but not with echo

[php] function displayComments($comments, $str){
foreach ($comments as $info) {
$str .= $info[‘id’];
if (!empty($info[‘childs’])) {
$this->displayComments($info[‘childs’],$str);
}
}
return $str;
}

$comments = $this->produceComments($id);
if(!$comments){
$str .=’


There are no comments for this Project
‘;
}else{
$str .= $this->displayComments($comments,’’);
}[/php]

$str .= $info[‘id’]; outputs 123
echo $info[‘id’]; outputs 125634 (which is the correct output)
array $comments is built fine.

Any idea why this would be outputting this way?

[code]Array
(
[1] => Array
(
[0] => 1
[id] => 1
[1] => 1
[project_id] => 1
[2] => 0
[parent] => 0
[3] => First post
[comment] => First post
[4] =>
[user] =>
[5] => 2014-02-01
[date] => 2014-02-01
[childs] => Array
(
)

    )

[2] => Array
    (
        [0] => 2
        [id] => 2
        [1] => 1
        [project_id] => 1
        [2] => 0
        [parent] => 0
        [3] => Second Post
        [comment] => Second Post
        [4] => 
        [user] => 
        [5] => 2014-02-01
        [date] => 2014-02-01
        [childs] => Array
            (
                [0] => Array
                    (
                        [0] => 5
                        [id] => 5
                        [1] => 1
                        [project_id] => 1
                        [2] => 2
                        [parent] => 2
                        [3] => Reply to 2nd post
                        [comment] => Reply to 2nd post
                        [4] => 
                        [user] => 
                        [5] => 2014-02-05
                        [date] => 2014-02-05
                        [childs] => Array
                            (
                            )

                    )

                [1] => Array
                    (
                        [0] => 6
                        [id] => 6
                        [1] => 1
                        [project_id] => 1
                        [2] => 2
                        [parent] => 2
                        [3] => Reply to 2nd post
                        [comment] => Reply to 2nd post
                        [4] => 
                        [user] => 
                        [5] => 2014-02-05
                        [date] => 2014-02-05
                        [childs] => Array
                            (
                            )

                    )

            )

    )

[3] => Array
    (
        [0] => 3
        [id] => 3
        [1] => 1
        [project_id] => 1
        [2] => 0
        [parent] => 0
        [3] => Reply to first post
        [comment] => Reply to first post
        [4] => 
        [user] => 
        [5] => 2014-02-19
        [date] => 2014-02-19
        [childs] => Array
            (
                [0] => Array
                    (
                        [0] => 4
                        [id] => 4
                        [1] => 1
                        [project_id] => 1
                        [2] => 3
                        [parent] => 3
                        [3] => Reply to first reply
                        [comment] => Reply to first reply
                        [4] => 
                        [user] => 
                        [5] => 2014-02-05
                        [date] => 2014-02-05
                        [childs] => Array
                            (
                            )

                    )

            )

    )

)[/code]

I would hazard a guess even though you are displaying it one way, it is actually stored a different way than you think it is. You are concatenating the display $str .= and probably storing it in the database a different way, but this is only a guess for you don’t show the full code.

$str is not being used in a database
echo $str after it and its 123

echo $info[‘id’]; is in the same loop as $str .= $info[‘id’];
where echo outputs 1 at a time “1,2,5,6,3,4”
$str .= should store each number in a row yet it only stores “1,2,3”
All I’m doing with $str is outputing it at the end anyhows… it’s just strange and am wondering why

[php] function displayComments($comments,$str=FALSE){
foreach ($comments as $info) {
$str .= $info[‘id’];
if (!empty($info[‘childs’])) {
$this->displayComments($info[‘childs’]);
}
}
echo $str;
return $str;
}[/php]

echo $str; outputs 564123 which is out of order.
And when I return $str;
then echo it
[php]$comments = $this->produceComments($id);
if(!$comments){
$str .=’


There are no comments for this Project
‘;
}else{
echo $this->displayComments($comments,’’);
	}[/php]

It produces 123…
I’m confused

Sponsor our Newsletter | Privacy Policy | Terms of Service