N
N
Name2021-06-26 11:06:58
PHP
Name, 2021-06-26 11:06:58

How to create another array from a similar array?

have an array

print_r ($matches[0]);
Array
(
    [0] => <li>a</li>
    [1] => <li>b</li>
    [2] => <li>c</li>
)

you need to get an array like this:
$trans = array("<h2>a</h2>" => "<h2 id="0">a</h2>",
"<h2>b</h2>" => "<h2 id="1">b</h2>",
"<h2>c</h2>" => "<h2 id="2">c</h2>");

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lillipup, 2021-06-26
@User782

$list = [
    '<li>a</li>',
    '<li>b</li>',
    '<li>c</li>',
];

$trans = [];

foreach ($list as $index => $item) {
    $item = strip_tags($item);
    $trans['<h2>'.$item.'</h2>'] = '<h2 id="'.$index.'">'.$item.'</h2>';
}
print_r($trans);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question