F
F
freeman02042018-03-14 01:15:12
PHP
freeman0204, 2018-03-14 01:15:12

How to split the string correctly in this case?

There is a field:
5aa84c13dc9d0770026906.jpeg
There is a select:

<select name="">
                                <?php
                                $type_service = get_field('type_service', 15);
                                $arr_type_service = explode(" ", $type_service);
                                foreach ($arr_type_service as $i) { ?>
                                    <option value="<?php echo $i ?>"><?php echo $i ?></option>
                                <?php } ?>
                            </select>

Here's what comes out:
5aa84c819ea6f083014564.jpeg
I need it to be like this:
<option value="300">Услуга</option>
    <option value="400">Услуга два</option>

How can this be implemented?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Igor Vorotnev, 2018-03-14
@freeman0204

No need to break anything, you are using the ACF plugin incorrectly. Change the values ​​for the select to:

300 : Услуга один
400 : Услуга два

1. Pay attention to the spaces around the colon
2. The first is the value attribute, the second is the label (human-readable text)
Well, on the output, you need to work with an associative array:
$type_service = get_field('type_service', 15);
foreach ( $type_service as $key => $value ) {
    echo "<option value=\"{$key}\">{$value}</option>"; // <option value="300">Услуга один</option>
}

D
Denis, 2018-03-14
@sidni

<?php foreach ($arr_type_service as $i) { ?>
$j = explode(':',$i);
<option value="<?php echo $j[1] ?>"><?php echo $j[0] ?></option>
<?php } ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question