如何在 Magento 中检索多个复选框的值

How to retrieve Multiple Checkbox value in Magento

我尝试显示数据库中的值但显示失败。,

首先我使用复选框插入值。,

代码如下,

$fieldset->addField('city', 'checkboxes', array(
                'label' => $this->__('City'),
                'name' => 'city[]',
                'required' => true,
                "checked" => $city,
                'values' => array(
                    array('value' => '0', 'label' => 'aaaaa'),
                    array('value' => '1', 'label' => 'bbbbbbb'),
                    array('value' => '2', 'label' => 'ccccccc'),
                    array('value' => '3', 'label' => 'dddddddd'),
                    array('value' => '4', 'label' => 'eeeeeeee')
                ),
                'onclick' => "",
                'onchange' => "",
                'disabled' => false,
                'value'  => '1',
                'tabindex' => 1
            )); 

之后,

$city = $post_data['city'] = implode(',', $post_data['city']);

使用上面的代码成功插入 like(0,1,2),

 if ($object->getData('city')) {
                $city = $object->getData('city');
                $city = explode(",", $city);
                 //var_dump($city);die;
            }

inserted value 我获取成功了, 现在我想显示勾选的(0,1,2)

哪位大神帮忙解决一下!!!!!!

创建一个新函数来获取选中的值,

类似代码

$fieldset->addField('city', 'checkboxes', array(
                    'label' => $this->__('City'),
                    'name' => 'city[]',
                    'required' => true,
                    "checked" => $city,
                    'values' => $this->getOptionValues($city),
                    'onclick' => "",
                    'onchange' => "",
                    'disabled' => false,
                    'value'  => '1',
                    'tabindex' => 1
                ));

public function getOptionValues($city)
    {
        $result = array();
        $selectedvalues = $city; //exploded array values(array(0=>0,1=>1,2=>2))
        $optionslists = array(
            array('value' => '0', 'label' => 'aaaaa'),
            array('value' => '1', 'label' => 'bbbbbbb'),
            array('value' => '2', 'label' => 'ccccccc'),
            array('value' => '3', 'label' => 'dddddddd'),
            array('value' => '4', 'label' => 'eeeeeeee')
        );
        if (count($selectedvalues) > 1) {
            foreach ($selectedvalues as $selectedvalue) {
                foreach ($optionslists as $optionslist) {
                    if (in_array($selectedvalue, $optionslist)) {
                        $result[] = $optionslist;
                    }

                }
            }
        } else {
            $result = $optionslists;
        }
    return $result;
    }