Question

In Magento, as usually we used to get param

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

we can get the param by

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

now as far as i know the $x just to get a string from the param.

is there any way to get $x as an array?

as example:

Array
(
    [0] => 122
    [1] => 233
)
Was it helpful?

Solution 2

I am afraid it is currently not possible for Zend Framework or Magento to pass array param to zend url.

Here is a bug reported on passing get variable as an array.

OTHER TIPS

For example:

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

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

If you mean get all of the parameters as an array (might be a varien object):

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

you can also use brackets on your query params, like http://magento.com/customer/account/view/?id[]=123&id[]=456

then after running the following, $x will be an array.

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

My suggestion if you want inputs from visitor as array then instead of passing them in URL as GET params pass those as POST variable.

Then $_POST will have all and you can $params = $this->getRequest()->getParams();

With Zend Framework 1.12 this is possible with just the method getParam(). Notice the result of getParam() is NULL for no available key, a string for 1 key, and an array for multiple keys:

No 'id' value

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

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

Single 'id' value

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

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

Multiple 'id' values:

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" }

This could be problematic if you always expect a string in your code, and for some reason more values are set in the url: In some cases you can for example run into the error "Array to string conversion". Here are some tricks to avoid such errors, to make sure you always get the type of result you need from getParam():

If you want the $id to be an array (or NULL if param is not set)

$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" }

If you always want the $id to be an array (no NULL value if not set, just empty array):

$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" }

Same as above in one line (no NULL, always array):

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

If you want the $id to be a string (the first available value, keeps NULL intact)

$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"

If you want the $id to be a string (the last available value, keeps NULL intact)

$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"

Maybe there are shorter/better ways to fix these 'response-types' of getParam, but if you are going to use the above scripts, it could be cleaner to create another method for it (extended helper or something).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top