2013-03-16 19 views
1

在Magento的,因为通常我们用来获取PARAM如何getParams阵列的类型

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

例如:

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

$x = $this->getRequest()->getParam('id'); 
$arrayQuery = array_map('intval', explode('-', $x))); 
var_dump($arrayQuery); 
3

如果你的意思是让所有的参数作为数组(可能是瓦瑞恩对象):

$params = $this->getRequest()->getParams(); 
2

我的建议,如果你想从访问者的数组作为数组,然后而不是在URL中传递它们,因为GET PARAMS将这些作为POST变量传递。

然后$ _POST将所有,你可以$ params = $ this-> getRequest() - > getParams();

3

你也可以用你的查询参数支架,像http://magento.com/customer/account/view/?id[]=123 & ID [] = 456

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

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

使用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" 

多“身份证”值:

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中设置:在某些情况下,例如,你可以运行进入错误“Array to string conversion”。这里有一些技巧,以避免这样的错误,以确保您始终获得结果你getParam()不只需要的类型:

如果你想在$ ID是一个阵列(或NULL如果param未设置)

$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的'响应类型',但是如果你要使用上面的脚本,可以更清洁地为它创建另一个方法(扩展的助手或其他)。