A
A
ART42020-05-11 13:55:10
PHP
ART4, 2020-05-11 13:55:10

Split array with newline for select?

Good afternoon, tell me please.
There is an array
5eb92e3cbdf31384395468.jpeg
It is necessary to break into lines and display in select.
Each line, a separate option item in select

The code itself:

$result = array();
$line = $new_info->city_countruies; //Moscow Saint-petersburg Krasnodar (с новой строки)
$ar = explode("\n",$line);
                    
foreach($ar as $arr){
        $lin = explode("\n",$arr);
        $result = $arr; 
}

$delivery->city = $result; // Вывод


What's wrong here? The last element appears and not all

HTML:
<select class="form-control" name="country">
    <option value="">{$delivery->city|escape}</option>
</select>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Daria Motorina, 2020-05-11
@ART4

You are overwriting $result on each iteration here, so it outputs the last element

foreach($ar as $arr){
        $lin = explode("\n",$arr);
        $result = $arr; //ошибка
        $result[] = $arr; //добавление нового элемента в массив
}

D
Dmitry Gordinskiy, 2020-05-11
@DmitriyGordinskiy

In the template, the output must be done through a loop:
Smarty foreach

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question