如何将特定键添加到数组值?

How to add specific key to array values?

我存储了指向 $fotolar 变量的链接,并使用 explode 转换为数组。

我的代码:

$ex = explode('<br>', $fotolar);

echo "<pre>";
print_r($ex);
echo "</pre>";

输出:

Array
(
    [0] => 
    https://www.mersevkids.com/uploads/images/202112/img_1920x_61b1f9e4ed8c48-88505152-13660630.jpeg
    [1] => 
    https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a438951a1-13859326-45341947.jpg
    [2] => 
    https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a3f2df271-08655153-10027442.jpg
)

如何将“url”键添加到此数组?

例如:

    Array
    (
        [0] => Array
(
           [url] =>
        https://www.mersevkids.com/uploads/images/202112/img_1920x_61b1f9e4ed8c48-88505152-13660630.jpeg
)
        [1] => Array
(
           [url] =>
        https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a438951a1-13859326-45341947.jpg
)
        [2] => Array
(
           [url] =>
        https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a3f2df271-08655153-10027442.jpg
)
    )

将字符串分解为数组后,您可以循环遍历此数组以创建一个新数组,如下所示:

<?php

$str = <<<STR
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b1f9e4ed8c48-88505152-13660630.jpeg<br>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a438951a1-13859326-45341947.jpg<br>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a3f2df271-08655153-10027442.jpg
STR;

$arr = explode('<br>', $str);
$result = [];

foreach ($arr as $v) {
    $result[]['url'] =  $v;
}

var_dump($result);

这是为您准备的一款衬垫,使用 array_map

$str = <<<STR
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b1f9e4ed8c48-88505152-13660630.jpeg<br>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a438951a1-13859326-45341947.jpg<br>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a3f2df271-08655153-10027442.jpg
STR;

$array = array_map(function($i){return['url'=>$i];},explode('<br>',$str));

Sandbox