-2

I stored links to $fotolar variable and I converted to array with explode.

My code:

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

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

Output:

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
)

How can I add "url" key to this array?

For example:

    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
)
    )
  • 1
    You could use `foreach` as you tagged. Have you tried anything yet? If so please add to question, that would help us, help you. – user3783243 Dec 29 '21 at 15:10
  • Create a new empty array, loop this one, and in it push every value with key to new array... – ikiK Dec 29 '21 at 15:12
  • Does this answer your question? [How to add keys to php array?](https://stackoverflow.com/questions/45214378/how-to-add-keys-to-php-array) Also google: add key to php array, this is duplicte – ikiK Dec 29 '21 at 15:15
  • 1
    @ikiK Don't even need new array. – user3783243 Dec 29 '21 at 15:15
  • @user3783243 I agree, first thing that come to my mind, but found a other solution in under a min here on SO... Haven't work with PHP for a while. – ikiK Dec 29 '21 at 15:17
  • @ikiK this is not duplicate. This is different issue. Thx. – Arda Ustundag Dec 29 '21 at 15:31

2 Answers2

0

After exploded string to array, you can loop through this array to create a new array like this:

<?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);
Sang Lu
  • 308
  • 3
  • 10
  • (Can also write as `$result[]['url'] = $v;` and it will be compatible with versions people shouldn't be using anymore) – user3783243 Dec 29 '21 at 15:22
0

Here is a one liner for you using 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

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38