2

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.

2
  • First you say if it is NULL or empty you want to set the variable, then later you say if it equals 1 you want to set the variable. Which is it? Commented Apr 9, 2020 at 2:38
  • 1
    if (isset($key['is_captain'])){ is obviously wrong and should issue an error. You probably meant to do if (isset($value['is_captain'])){ Commented Apr 9, 2020 at 2:47

2 Answers 2

1
$arr = array(
  6 => array(
    'element' => 191,
    'position' => 7,
    'multiplier' => 2,
    'is_captain' => 1,
    'is_vice_captain' => null
  ),
  7 => array(
    'element' => 171,
    'position' => 8,
    'multiplier' => 1,
    'is_captain' => null,
    'is_vice_captain' => null
  )
);

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 to 1 -> $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 key is_captain and that keys value is equal to 1 get the value of the key set as element and assign it to your variable:

    if ($key === 'is_captain' && $val === 1)){
      $variable = $value['element'];
    }
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the guidance. Thoroughly appreciated. What's weird is that I implemented your tips, wrote a block that looks like this: foreach ($captainsArr as $value) { foreach ($value as $index => $data) { if ($index === 'is_captain' && $data === 1){ $captain = $value['element']; } } } But my new variable $captain isn't recognized outside the loop. Nor can I echo it from inside.
is the loop inside a function or something?
No, I ensured that the foreach loop is on its own. I'll keep hacking away. Thanks for your help, Dale.
If I remove the if statement inside the loop, the $variable is set and I can access it outside the loop. It's the wrong value of course, but at least it exists and can be used. So it seems to be something with the conditional.
1

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'];
                }
            }
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.