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'];
}
Related
This question already has answers here:
PHP: Get the key from an array in a foreach loop [duplicate]
(3 answers)
Closed 7 years ago.
This seems simple enough, but my googling seems to be off to get the right answer:
I am dynamically making an array
$data = array();
$data['this is text'] = 1312;
$data['heres more text'] = 191;
$data['im also a unique entry'] = 239
I then want to go through each item and get the key and the value:
foreach($data as $datapoint)
{
var_dump($datapoint); //this seems to just be the value
$k = key($garavg); //this does not work because datapoint is a value not an array
}
I simply want to be able to do this:
echo "$key has this many entries $value";
foreach has that built in:
foreach($data as $key => $value )
{
echo "$key has this many entries $value";
}
This question already has answers here:
PHP: Get the key from an array in a foreach loop [duplicate]
(3 answers)
Closed 7 years ago.
After years of confusion I am trying to get to grips with PHP arrays and especially in combination with foreach loops.
There are various similar questions but I am really trying to understand why this is NOT working rather than make it work per se (will be using lots of foreach loops and arrays in more complicated stuff shortly).
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"37");
Have a simple foreach loop:
foreach ($age as $ages )
{
n();n();
echo($ages);
n();
$v="volvo";
$key = key($age);
if ($age["Ben"]==$ages)
{
echo "<BR><BR>Result is $key is $ages <BR>";
}
}
Output is:
35
37
Result is Ben is 37
37
Result is Ben is 37
All fine but I was expecting the last name to be Joe.
I thought that a foreach looped through each array value pair as a KEY VALUE pair. So why am I getting Ben twice?
Use foreach in the following way: foreach ($age as $name=>$personAge )
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
This question already has answers here:
PHP Recursively unset array keys if match
(11 answers)
Closed 9 years ago.
Say I have an array that looks somewhat like this: array("a"=>array("a"=>"b"), "b"=>array("a"=>"d")).
I would like to unset all vars with the key "a" within the array and it sub-arrays. Assume that the data's structure is unknown. All I want is that if the key "a" exists somewhere within the parent array or it sons - it'll be unset. Is it possible?
function unsetKey (&$array, $key) {
foreach ($array as $k => $v)
if (is_array($v))
unsetKey($array[$k], $key);
if (isset($array[$key])) unset(array[$key]);
}
That should do it.
This question already has answers here:
How to access "key" and "value" from an array passed to a for loop?
(5 answers)
Closed 8 years ago.
I want to print out all of the values in my array, however the keys for the elements are strings. How do I make a loop that will print out all of the elements in my array if the keys are strings and not ints?
Here is my code so far: http://codepad.viper-7.com/xGhmhX
You could use a foreach construct :
foreach ($your_array as $key => $value) {
// Work with either $key or $value
}
Or, if you don't need the key inside the loop, you can just use :
foreach ($your_array as $value) {
// Work with $value
}