我有一系列我想安排的自定义挂钩,按照这些方式构建:
public function __construct(WPSM_Logger $injected_logger = null) {
$this->cron_hooks[WPSM_CRONHOOK_SENDQUEUE] = array ("frequency" => 60);
}
然后,在我的Scheduler类的构造函数中,我想遍历$ this-> cronhooks并安排该数组中的每个钩子。我通常更喜欢双引号字符串中的直接和简单的变量扩展,就像我在下面用$ name做的那样,但我似乎无法弄清楚如何用数组和下标来做这个。
foreach ($this->cron_hooks as $name => $options) {
$freq = $options["frequency"];
echo "Hook '$name' will run every $freq seconds.";
}
我想取消中间$freq = $options["frequency"];
行,并在echo
行中有类似内容:
foreach ($this->cron_hooks as $name => $options) {
$freq = $options["frequency"];
echo "Hook '$name' will run every $options['frequency]seconds.";
}
然而,我根本无法让它发挥作用。有什么特别的东西我不知道,还是我真的要把一个额外的变量拖到我的派对上?
答案 0 :(得分:7)
foreach ($this->cron_hooks as $name => $options) {
echo "Hook '$name' will run every ${$options['frequency']} seconds.";
}
答案 1 :(得分:5)