开发者

Adding values to an array

开发者 https://www.devze.com 2023-04-05 15:23 出处:网络
I what to get a value returned from a object method and put it into an array. The codes is as follows:

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/AppointmentT开发者_开发知识库ime.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:

foreach($additionalTests as $availableTest) {
  $additionalTestConfirmation[] = $availableTest->getName();
  // or array_push($additionalTestConfirmation, $availableTest->getName()); 
  // see: http://us3.php.net/array_push
}

See the docs for more: http://php.net/manual/en/language.types.array.php

EDIT

Also, on this line:

'%AdditionalTestsSelected%' => $additionalTestConfirmation,

... 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 $tokens array will do and expect. If it expects a plain string for the AdditionalTestsSelected index, you might need to do this:

'%AdditionalTestsSelected%' => implode(',', $additionalTestConfirmation)

... to make the value a comma-delimited list.

Also note, at the end of that line you have an extra comma.


In order to add each $availableTest->getName() to the array $additionalTestConfirmation you need to use [] on your array and the assignment operator =

foreach($additionalTests as $availableTest)
{
  $additionalTestConfirmation[] = $availableTest->getName();

}

You can also use the function array_push


foreach($additionalTests as $availableTest)
{
  $additionalTestConfirmation[] = $availableTest->getName();

}

after your comment I propose you this:

$appointment = $this->getAppointment();

$token = array(
  '%DATE%' => $appointment->getDate(),
  '%LOCATION%' => $appointment->getLocation(),
  '%TIME%' => $this->getTime(),
  '%ROOM%' => $this->getRoom(),
  '%NAME%' => ($this->getUser() ? $this->getUser()->getFullName() : null),
  '%TZ%'   => $appointment->getShowTimeZone() ? $appointment->getTimeZone() : ''
  );

$tokens = array();

$additionalTests = $this->getAdditionalTestsSelected();

foreach($additionalTests as $availableTest)
{
  $token['%AdditionalTestsSelected%'] = $availableTest->getName();
  $tokens[] = $token;
}

// here comes logic for all tokens
0

精彩评论

暂无评论...
验证码 换一张
取 消