Why I'm getting just data's first letters when I'm writing array?

I've an array $new_data, and result of print_r($new_data['comments']); is as follows

Array (
    [id] => 1 
    [uye] => yusufalibozkir 
    [yorum] => Saffet gel beni affet 
    [post] => 1 
)

But when I want to print my datas with using foreach loop:

foreach($new_data['comments'] as $comments)
{
    echo $comments['id'];
}

The result:

1yS1 

Any help will be appreciated.

EDIT:

This question might be like below I'm creating array like below:

$comments = $this->post_model->getComments($data['id']);
            foreach($comments as $comments)
            {
                $comments['uye'] = $this->members_model->lognick($comments['uye']);
            }

Actually, It need to have 2 array inside of main array. Because my table has 2 rows. But It returns just last data.

Answers


$comments = $this->post_model->getComments($data['id']);
foreach($comments as $comments)
{
    $comments['uye'] = $this->members_model->lognick($comments['uye']);
}

This code is an example of unexpected behavior of PHP. Notice foreach($comments as $comments) both sides of as are same variable so eventually your original $comments variable is overwritten by last value of $comments inside foreach and is reduced to value array of the comment instead of being an array of comments, and you get

Array (
    [id] => 1 
    [uye] => yusufalibozkir 
    [yorum] => Saffet gel beni affet 
    [post] => 1 
)

instead of an array containing such arrays.

Fix it like follows

$comments = $this->post_model->getComments($data['id']);
foreach($comments as $key => $comm)
{
    $comments[$key]['uye'] = $this->members_model->lognick($comm['uye']);
}

and your original code should work fine.


the problem is when you do a foreach it is actually returning each object in the array, so when you are putting the index in, it's an index for the string... just remove the index in your echo

echo $comments;

Need Your Help

Error starting sidekiq on Heroku, missing sidekiq.log file crash

ruby-on-rails ruby-on-rails-4 heroku redis sidekiq

I'm new to using sidekiq & redis and am trying to get sidekiq to run on Heroku, but it seems to crash each time I deploy my Rails 4 app. I get this error in Papertrail:

oracle sql 'like' to find a specific substring

sql oracle like

Using oracle sql, I want to find a text containing, for example, the exact word 'SAN' or its plural if any.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.