Foreach returns only one row

谁都会走 提交于 2021-02-17 07:20:13

问题


I have foreach loop, but it only returns a single option in select menu. Vardump shows multiple results. What am I missing?

$vars = explode(';', $this->settings['address']);

foreach ($vars as $row) {
    return array(
        'title' => $this->name,
        'options' => array(
            array(
                'id' => 'option_1',
                'icon' => $this->settings['icon'],
                'name' => 'Option 1',
                'description' =>'',
                'fields' => '<select name="address" class="form-control">' . PHP_EOL
                . '<option value="'.$row.'" >'.$row.'</option>' . PHP_EOL
                . '</select>' ,
                'cost' => $this->settings['fee'],
                'tax_class_id' => $this->settings['tax_class_id'],
                'exclude_cheapest' => false,
            ),
        ),
    });
}

回答1:


try this

$vars = explode(';', $this->settings['address']);

$options = [];
foreach ($vars as $row) {
    $options[] = array(
        'id' => 'option_1',
        'icon' => $this->settings['icon'],
        'name' => 'Option 1',
        'description' =>'',
        'fields' => '<select name="address" class="form-control">' . PHP_EOL
            . '<option value="'.$row.'" >'.$row.'</option>' . PHP_EOL
            . '</select>' ,
        'cost' => $this->settings['fee'],
        'tax_class_id' => $this->settings['tax_class_id'],
        'exclude_cheapest' => false,
    );
}

return array(
    'title' => $this->name,
    'options' => $options
);

Or maybe this

$options = '';

foreach ($vars as $row) {
    $options .= '<option value="' . $row . '" >' . $row . '</option>' . PHP_EOL;
}

$fileds = '<select name="address" class="form-control">' . PHP_EOL . $options . '</select>';

return array(
    'title' => $this->name,
    'options' => array(
        array(
            'id' => 'option_1',
            'icon' => $this->settings['icon'],
            'name' => 'Option 1',
            'description' =>'',
            'fields' => $fileds,
            'cost' => $this->settings['fee'],
            'tax_class_id' => $this->settings['tax_class_id'],
            'exclude_cheapest' => false,
        ),
    ),
});



回答2:


$vars = explode(';', $this->settings['address']);
$myReturn = array();
foreach ($vars as $row) {
    array_push($myReturn, array(
        'title' => $this->name,
        'options' => array(
            array(
                'id' => 'option_1',
                'icon' => $this->settings['icon'],
                'name' => 'Option 1',
                'description' =>'',
                'fields' => '<select name="address" class="form-control">' . PHP_EOL
                . '<option value="'.$row.'" >'.$row.'</option>' . PHP_EOL
                . '</select>' ,
                'cost' => $this->settings['fee'],
                'tax_class_id' => $this->settings['tax_class_id'],
                'exclude_cheapest' => false,
            ),
        ),
    ));
}
return $myReturn;


来源:https://stackoverflow.com/questions/53854212/foreach-returns-only-one-row

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!