1

I am setting a array in my controller by:

$messages = array("Apples", "Oranges", "Pears");

$this->set('messages', "$messages");

However then in my View if I try to print the array with:

<?php
     print_r($messages);
?>

I just get the word 'Array' not the actual content of the array.

Trying to access elements of the array does not seem to work either, for example:

echo $messages[0];

gives 'A'

3 Answers 3

2

Remove double quote from $this->set('messages', "$messages");

it should be

$messages = array("Apples", "Oranges", "Pears");

$this->set('messages', $messages);

and in view you can

<?php
     pr($messages);
?>
Sign up to request clarification or add additional context in comments.

1 Comment

pr() maps to print_r() in Cake, but possibly even more useful is debug() which formats the outputted array and includes the line number where the print statement was included. :-)
0

Try passing true to print_r, and remove your quote from $messages.

$messages = array("Apples", "Oranges", "Pears");

$this->set('messages', $messages);
print_r($messages, true);

Comments

0

@Prakash is right, remove the double quote in "$messages" let it be $messages. So you have $this->set('$messages', $messages) and in your view you can just use <?php echo $messages[0] ?> You should be able to see "Apples". Or print_r $messages will give you:

[0] => "Apples",
[1] => "Oranges",
[2] => "Pears"

Comments

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.