【发布时间】:2017-06-03 11:35:59
【问题描述】:
我是 PHP OOP 的新手,我想知道如何将新项目从 setter 添加或推送到数组并显示所有项目,这是我的代码。
$r = "/some regex here/";
for ($i = 0; $i < count($p[0]); $i++)
{
preg_match($r, $text, $value);
preg_match($r, $schema, $name);
$this->setTheOutput('name', $name[1]);
$this->setTheOutput('value', $value[1]);
}
这是另一个类中的数组
protected $output = [];
protected function setTheOutput($name, string $value)
{
$this->output[$name] = $value;
}
结果应该是这样的
{ {{"name":"{{Name}}","value":"Foo"}, {"name":"{{Age}}","value":"20"}, {"name":"{{mission}}","value":"none"}}, { {{"name":"{{Name}}","value":"Bar"}, {"name":"{{Age}}","value":"25"}, {"name":"{{mission}}","value":"none"}} }
每当我遍历 For 循环时,我总是得到最后一个项目,我想得到所有项目并像结果中提到的那样显示它们。
【问题讨论】:
-
setter 覆盖现有数据,因此您必须收集数据并在循环后设置它
-
是的,但我该怎么做
标签: php arrays class for-loop setter