1

在 Magento 中,就像我们通常用来获取参数一样

http://magento.com/customer/account/view/id/122

我们可以通过

$x = $this->getRequest()->getParam('id');
echo $x; // value is 122

现在据我所知 $x 只是为了从参数中获取一个字符串。

有没有办法将 $x 作为一个数组?

例如:

Array
(
    [0] => 122
    [1] => 233
)
4

6 回答 6

5

例如:

http://magento.com/customer/account/view/id/122-233

$x = $this->getRequest()->getParam('id');
$arrayQuery = array_map('intval', explode('-', $x)));
var_dump($arrayQuery);
于 2013-03-16T02:24:34.787 回答
3

如果您的意思是将所有参数作为一个数组(可能是一个变量对象):

$params = $this->getRequest()->getParams();
于 2013-03-16T02:26:56.940 回答
3

恐怕 Zend Framework 或 Magento 目前无法将数组参数传递给 zend url。

这是一个关于将 get 变量作为数组传递的错误。

于 2013-03-16T02:37:42.437 回答
3

您还可以在查询参数上使用括号,例如 http://magento.com/customer/account/view/?id[]=123&id[]=456

然后在运行以下命令后, $x 将是一个数组。

$x = $this->getRequest()->getParam('id');
于 2013-08-23T16:42:55.547 回答
2

我的建议是,如果您希望将访问者的输入作为数组传递,而不是将它们作为 GET 参数传递到 URL 中,将它们作为 POST 变量传递。

然后 $_POST 将拥有一切,您可以 $params = $this->getRequest()->getParams();

于 2013-03-16T04:36:42.817 回答
1

在 Zend Framework 1.12 中,只需使用 getParam() 方法即可。注意 getParam() 的结果是NULL 表示没有可用的键字符串表示 1 个键数组表示多个键

没有“id”值

http://domain.com/module/controller/action/

$id = $this->getRequest()->getParam('id');
// NULL

单个“id”值

http://domain.com/module/controller/action/id/122

$id = $this->getRequest()->getParam('id');
// string(3) "122"

多个“id”值:

http://domain.com/module/controller/action/id/122/id/2584

$id = $this->getRequest()->getParam('id');
// array(2) { [0]=> string(3) "122" [1]=> string(4) "2584" }

如果您总是希望代码中有一个字符串,并且由于某种原因在 url 中设置了更多值,这可能会出现问题:在某些情况下,您可能会遇到错误“数组到字符串转换”。以下是避免此类错误的一些技巧,以确保您始终从 getParam() 获得所需的结果类型:

如果您希望 $id 是一个数组(如果未设置参数,则为 NULL)

$id = $this->getRequest()->getParam('id');
if($id !== null && !is_array($id)) {
    $id = array($id);
}

http://domain.com/module/controller/action/
// NULL

http://domain.com/module/controller/action/id/122
// array(1) { [0]=> string(3) "122" }

如果您总是希望 $id 是一个数组(如果未设置则没有 NULL 值,只是空数组):

$id = $this->getRequest()->getParam('id');
if(!is_array($id)) {
    if($id === null) {
        $id = array();
    } else {
        $id = array($id);
    }
}

http://domain.com/module/controller/action/
// array(0) { }

http://domain.com/module/controller/action/id/122
// array(1) { [0]=> string(3) "122" }

与上面一行相同(无 NULL,始终为数组):

$id = (array)$this->getRequest()->getParam('id');

如果您希望 $id 是一个字符串(第一个可用值,保持 NULL 不变)

$id = $this->getRequest()->getParam('id');
if(is_array($id)) {
    $id = array_shift(array_values($id));
}

http://domain.com/module/controller/action/
// NULL

http://domain.com/module/controller/action/122/id/2584
// string(3) "122"

如果您希望 $id 是一个字符串最后一个可用值,保持 NULL 不变)

$id = $this->getRequest()->getParam('id');
if(is_array($id)) {
    $id = array_pop(array_values($id));
}

http://domain.com/module/controller/action/
// NULL

http://domain.com/module/controller/action/122/id/2584/id/52863
// string(5) "52863"

也许有更短/更好的方法来修复 getParam 的这些“响应类型”,但如果您要使用上述脚本,为它创建另一种方法(扩展助手或其他东西)可能会更干净。

于 2015-06-05T10:31:55.113 回答