This question already has answers here:
PHP foreach change original array values [duplicate]
(5 answers)
Closed 6 years ago.
I have the following code that loops trough an array called projects and each project is an associative array. Then I get the image properties and then I want to add a new element to this associative array with the image properties. But it doesn't get added.
foreach ($projects as $project) {
$image_dimensions = array(getimagesize('data/'.$project['base_image']));
$project['image_dimensions'] = $image_dimensions;
}
Why isn't $project['image_dimensions'] getting added to $project?
Please try with this one.
You need to add with all projects all key.
foreach ($projects as $key => $project) {
$image_dimensions = array(getimagesize('data/'.$project['base_image']));
$projects[$key]['image_dimensions'] = $image_dimensions;
}
Related
This question already has answers here:
Two arrays in foreach loop
(24 answers)
Closed 3 years ago.
I am getting data into two arrays and i want to push that data into single array one by one. Let say i want to get first element from first array and first element from second array and then push that data into single array.
first array
$url = $a->attr['href'];
$lnk[] = ['url'=>$url];
second array
$img = $img->attr['src'];
$img[] = ['img'=>$img];
I want this
$data[] = ['url'=>$url, 'img'=>$img];
I assume the size is same so you can iterate over the length on any of the arrays.
$finalArray=[];
for($i=0;$i<count($lnk);$i++){
$finalArray[] = ['url'=>$lnk[$i]['url'], 'img'=>$img[$i]['img']];
}
This question already has answers here:
Loop a multidimensional array and only print two specific column values per row
(6 answers)
Closed 4 years ago.
The [1152] key is dynamic.
I need to access the child keys (ex: guid) but the parent key [1152] is dynamic and there is no way for me to know which number is going to be generated.
Is it possible to access [guid] without knowing what the number is in the parent key?
Yes process the first level of the array in a simple foreach and then the inner array specifically like this
foreach ($array as $dynamic) {
foreach ($dynamic as $key=>$val) {
echo $key . ' = ' . $val;
}
}
Or even more simply
foreach ($array as $dynamic) {
echo $dyanmic['guid'];
}
This question already has answers here:
PHP Remove elements from associative array
(9 answers)
Closed 5 years ago.
The title says it all. I want to delete the highlighted section in yellow as shown in picture below. And rest remain unchanged. What is the best way to do it? Is there a method that does't use foreach?
you can do this just with one foreach!
foreach ($data as $key => $subArr) {
unset($subArr['id']);
$data[$key] = $subArr;
}
You can use the following
$filteredArray = array_map(function($array) {
unset($array['id']);
return $array;
}, $dataArray);
Instead of doing foreach() loop on the array, You can go with array_search()
$results=array_search($unwantedValue,$array,true);
if($results !== false) {
unset($array[$result]);
}
This question already has answers here:
PHP - Grab the first element using a foreach
(10 answers)
Closed 5 years ago.
how can I get first item from db by foreach
$posts = new Posts();
$post = $posts->feature_post($conn);
foreach($post as $feature) { ?>
my html code is different for 1st item. so I need to get 1st item then other item,
how I can do it?
thanks in advance.
To get the first item of an array, you can use the reset function
http://php.net/manual/fr/function.reset.php
<?php
$posts = new Posts();
$listPost = $posts->feature_post($conn);
$firstPost = reset($listPost);
...
Also if you want to know if you loop through the first element and if the keys of your arrays are 0,1,2,3 etc..
<?php
foreach($array as $key => $cell) {
if ($key === 0) {
// this is your first element
....
}
}
If the keys of your array are not numeric indexes but you don't intend to use them, you can obtain such array by using the array_values function
This question already has answers here:
get a single value of array items in php
(3 answers)
Closed 7 years ago.
I am having a bit of trouble getting a single item shown/echoed in a foreach loop. What I mean I have an array (filled from database).
return array(
'FriendName' => $FriendName,
'FriendImage' => $FriendImage
);
What I want is to show only FriendName while in a loop.
I have tried and this is an example:
foreach($UserFriends as $item) {
echo $item['FriendName'];
}
But it shows NULL and not the data I expect.
No need for a loop if you just need one value, you can use this instead:
echo $UserFriends['FriendName']
Learn more about php Arrays
Its been better to use it directly instead of looping an array if its not multidimensional array you can simply get it using
$UserFriends['FriendName']
you can checked using if condition like
foreach($UserFriends as $key => $item) {
if($key == 'FriendName'){
echo $item;
}
}
your array is not multidimensional so you can access vlaues like that.
or else you can get a value directly like
echo $userFriends['FriendName'];
I hope this is working for you