有没有更清洁的方法将数组从控制器发送到功能,模型或视图?

时间:2019-08-12 17:14:02

标签: php codeigniter

有没有更紧凑的方法将数组发送给函数?

这是我目前正在做的事情:

$data = array(
    'id' => '1'
    );
$result = $this->Tests_model->DoSomething($data);

我想做的只是:

$result = $this->Tests_model->DoSomething(array('id' => '1'));

$result = $this->Tests_model->DoSomething(('id' => '1'));

...但是我似乎无法格式化(和)内部的数据。我仍然想传递一个数组,以实现更好的代码重用。有办法吗?

1 个答案:

答案 0 :(得分:0)

此行

$result = $this->Tests_model->DoSomething(array('id' => '1'));

完全有效并且可以使用。

您需要更改第三个示例才能使其工作。您正在寻找的是

$result = $this->Tests_model->DoSomething(['id' => '1']);

语法['id' => '1']只是编写array('id' => '1')的另一种方式,它们可以产生确切的效果。

创建数组并向其添加键/值对的最后一种方法是这样的。

$data['id'] = '1';  //assigns a value of '1' to the key 'id' in an array named $data.

以上只是写作的另一种语法。

$data = array('id' => '1');

除了“可读性”方面的问题外,这些语法上的变化都没有好坏之分。

关于PHP Documentation中数组的所有知识。