Kevin
Kevin

Reputation: 61

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 $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.

Upvotes: 2

Views: 791

Answers (2)

Kevin
Kevin

Reputation: 61

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

Upvotes: 1

dale landry
dale landry

Reputation: 8610

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

Upvotes: 1

Related Questions