I what to get a value returned from a object method and put it into an array. The codes is as follows:
$additionalTestConfirmation = array();
$additionalTests = $this->getAdditionalTestsSelected();
foreach($additionalTests as $availableTest)
{
$additionalTestConfirmation = $availableTest->getName();
}
$appointment = $this->getAppointment();
$tokens = array(
'%DATE%' => $this->getAppointment()->getDate(),
'%LOCATION%' => $this->getAppointment()->getLocation(),
'%TIME%' => $this->getTime(),
'%ROOM%' => $this->getRoom(),
'%NAME%' => ($this->getUser() ? $this->getUser()->getFullName() : null),
'%TZ%' => $this->getAppointment()->getShowTimeZone() ? $this->getAppointment()->getTimeZone() : '',
'%AdditionalTestsSelected%' => $additionalTestConfirmation,
);
For the codes above, I got a system error message: Notice: Array to string conversion in /Users/alexhu/NetBeansProjects/menagerie/svn/trunk/apps/frontend/modules/legacy/legacy_lib/lib/classes/AppointmentTime.php on line 379.
How do I avoid this and get the $availableTest->getName() returned value I want. thanks
When you assign elements to an array, you must either specify an index, or use empty square brackets (
[]) to add an item:See the docs for more: http://php.net/manual/en/language.types.array.php
EDIT
Also, on this line:
… you are passing an array into this index. If the code afterword expects this to be a string, that could cause the errors. *This is not causing the error – it is perfectly acceptable to put an array in another array. As I mentioned, though, it really depends on what the code that uses the
$tokensarray will do and expect. If it expects a plain string for theAdditionalTestsSelectedindex, you might need to do this:… to make the value a comma-delimited list.
Also note, at the end of that line you have an extra comma.