I'm new.
I have a situation where I need to loop through an array, determine if a $key in that array has a value of 1, then set a variable with the $value from different $key in the same array.
Here's what I mean.
I retrieve a JSON array from an API that looks, in part, like this:
(
[6] => Array
(
[element] => 191
[position] => 7
[multiplier] => 2
[is_captain] => 1
[is_vice_captain] =>
)
[7] => Array
(
[element] => 171
[position] => 8
[multiplier] => 1
[is_captain] =>
[is_vice_captain] =>
)
What I want to do is loop through the array, determine whether the key [is_captain] has a value (1), and set a variable using the value from a different $key, specifically [element].
For example, in the code above at [6], I want to create a variable with the value of [element] => 191 (191) if the value of [is_captain] is 1.
Here's where I left things:
for($i = 0; $i < count($players['picks']); $i++){
foreach ($fpl_team_picks['picks'][$keys[$i]] as $key => $value){
if (isset($key['is_captain'])){
$variable = $value['element'];
}
}
}
It doesn't work. I've tried the isset function and a series of array functions (array_column and others), and I'm stumped.
if (isset($key['is_captain'])){is obviously wrong and should issue an error. You probably meant to doif (isset($value['is_captain'])){