Foreach returns only one row

后端 未结 2 754
庸人自扰
庸人自扰 2021-01-29 14:33

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->sett         


        
相关标签:
2条回答
  • 2021-01-29 15:02

    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,
            ),
        ),
    });
    
    0 讨论(0)
  • 2021-01-29 15:18
    $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;
    
    0 讨论(0)
提交回复
热议问题