I want to add item to my array using loop. But i can't make it right.
I tried this code, and it can't gave me the Format/Output i want.
$img_arr[]= "";
foreach ($images as $image) {
$img_arr['src'][] .= $image['src'];
}
i want this kind of output.
array (
'src' => 'https://example.com/wp-content/uploads/2019/07/3.jpg',
),
array (
'src' => 'https://example.com/wp-content/uploads/2019/07/2-1.jpg',
),
array (
'src' => 'https://example.com/wp-content/uploads/2019/07/3.jpg',
),
array (
'src' => 'https://example.com/wp-content/uploads/2019/07/1-1.jpg',
),
array (
'src' => 'https://example.com/wp-content/uploads/2019/07/4-1.jpg',
)
Change you code a bit:
$img_arr = []; //create an array variable
foreach ($images as $image) {
//assign child-array to newly create array variable
$img_arr[] = ['src'=>$image['src']];
}
print_r($img_arr); //check desired output coming or not?
This code:
foreach ($images as $image) {
$img_arr['src'][] .= $image['src'];
}
adds a single array to $img_arr, with "src" as the key, and the URLs as the values. The period before the equals sign is also unnecessary.
You want this:
foreach ($images as $image) {
$img_arr[] = ['src' => $image['src']];
}
This adds a series of new arrays to $img_arr. Each array added is a key value pair, key = "src", value = [whatever the URL is].
$images = [
['src'=>'1.jpg'],
['src'=>'2.jpg'],
['src'=>'3.jpg'],
['src'=>'4.jpg'],
['src'=>'5.jpg'],
['src'=>'6.jpg']
];
$url = 'https://example.com/wp-content/uploads/2019/07/';
$img_arr = array();
foreach($images as $image){
$img_arr[] = ['src' => $url.$image['src']];
}
print_r($img_arr);
Output:
-------------------------------------------------------------------
Array
(
[0] => Array
(
[src] => https://example.com/wp-content/uploads/2019/07/1.jpg
)
[1] => Array
(
[src] => https://example.com/wp-content/uploads/2019/07/2.jpg
)
[2] => Array
(
[src] => https://example.com/wp-content/uploads/2019/07/3.jpg
)
[3] => Array
(
[src] => https://example.com/wp-content/uploads/2019/07/4.jpg
)
[4] => Array
(
[src] => https://example.com/wp-content/uploads/2019/07/5.jpg
)
[5] => Array
(
[src] => https://example.com/wp-content/uploads/2019/07/6.jpg
)
)
You initialize the array with $img_arr[] = "";
which will result in
array(1) {
[0]=>
string(0) ""
}
I think you meant $img_arr = [];
In the foreach you use .=
which is a string concatenation operator. You can omit the dot and use just []
to push an element to the end of the array.
Then adding to src
as the key in the foreach will give you an array with 2 keys where the first is 0 and is unused the the second is key
.
An alternative could be using array_map:
$img_arr = array_map(function ($x) {
return ['src' => $x["src"]];
}, $images);
print_r($img_arr);
Result
Array
(
[0] => Array
(
[src] => https://example.com/wp-content/uploads/2019/07/3.jpg
)
[1] => Array
(
[src] => https://example.com/wp-content/uploads/2019/07/2-1.jpg
)
[2] => Array
(
[src] => https://example.com/wp-content/uploads/2019/07/3.jpg
)
[3] => Array
(
[src] => https://example.com/wp-content/uploads/2019/07/1-1.jpg
)
[4] => Array
(
[src] => https://example.com/wp-content/uploads/2019/07/4-1.jpg
)
)