php - Set a variable using an array value based on another value in the same array

I'm new.
I have a situation where I need to loop through an array, determine if a{-code-1}
in that array has a value of1
, then set a variable with the$value
from different{-code-1}
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{-code-1}
, 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]
is1
.
Here's where I left things:
for($i = 0; $i < count($players['picks']); $i++){
foreach ($fpl_team_picks['picks'][{-code-1}s[$i]] as {-code-1} => $value){
if (isset({-code-1}['is_captain'])){
$variable = $value['element'];
}
}
}
It doesn't work. I've tried theisset
function and a series of array functions (array_column
and others), and I'm stumped.
Answer
Solution:
{-code-1}
Set foreach loop on the array, set the values, loop through values, find the key value,$index === 'is_captain'
and make sure it is set to1
->$data === '1'
. If this is true define your variable.
foreach($arr as $value){
foreach($value as $index => $data){
if($index === 'is_captain' && $data === 1){
$element = $value['element'];
echo $element; // $element now holds the value where key = `element` if 'is_captain' is equal to `1`
}
}
}
In your code, change the$key['is_captain']
to$key === 'is_captain'
then look for its value if it is a match with in that same conditional.
If the key is equal to target keyis_captain
and that keys value is equal to1
get the value of the key set aselement
and assign it to your variable:
if ($key === 'is_captain' && $val === 1)){
$variable = $value['element'];
}
Answer
Solution:
I was set in the right direction, so thanks immensely to the contributor for the help.
My original question didn't make clear that I was working with a nested array. My array snippet didn't show that. I've learned a lesson about completeness. (I'm new here).
Once I wrote the code block to handle the nested array and modified the conditional slightly, I was successful. Here's the final code which works:
$x = 0;
$captainsArr = array($fpl_team_picks['picks']);
foreach($captainsArr[$x++] as $value) {
if (is_array($value)){
foreach ($value as $index => $data) {
if ($index === 'is_captain' && $data == 1){
$captain = $value['element'];
}
}
}
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: the payload is invalid.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.