NightPorter
NightPorter

Reputation: 171

PHP Associative Arrays - how to match up values based on a condition

I'm passing a team name through a cookie from another page. When the user reaches the new page I check to see if that team name exists in my array if it does then do something, and if it doesn't then do something else. This works fine. However, if the condition is true I want to set the variable $team_color to the color from the nested array where the team name was found.

I can't figure out how to access this.

I'm currently trying to access array_values but with no luck. Any advice would be much appreciated.

Here is what i have so far -

if(isset($_COOKIE['TEAM']))

{

$team_cookie = $_COOKIE['TEAM'];

$team_info=Array (

    '0' => Array (

            'team_name' => 'team1',
            'team_color' => 'red'
        ),

    '1' => Array (

            'team_name' => 'team2',
            'team_color' => 'blue'
        ),

    '2' => Array (

            'team_name' => 'team3',
            'team_color' => 'green'
        ),

    '3' => Array (

            'team_name' => 'team4',
            'team_color' => 'yellow'
    )
);

if(in_array($team_cookie, array_column($team_info, 'team_name'))) { 


   $team_color = array_values($team_info, 'team_color');


    // Do something        

}

}

Upvotes: 0

Views: 1343

Answers (3)

Jaydip kharvad
Jaydip kharvad

Reputation: 1100

Try this code :

if(isset($_COOKIE['TEAM'])) {
    $team_cookie = $_COOKIE['TEAM'];
    $team_info=Array (

        '0' => Array (

                'team_name' => 'team1',
                'team_color' => 'red'
            ),

        '1' => Array (

                'team_name' => 'team2',
                'team_color' => 'blue'
            ),

        '2' => Array (

                'team_name' => 'team3',
                'team_color' => 'green'
            ),

        '3' => Array (

                'team_name' => 'team4',
                'team_color' => 'yellow'
        )
    );
    $color = ""; 
    foreach ($team_info as $team_info) {
       if($team_cookie == $team_info['team_name'] ) {
        // Do something  
        $color = $team_info['team_color'];
        break;
       }
    }
    echo $color;
}

Upvotes: 1

nice_dev
nice_dev

Reputation: 17805

So, to get the team color for a team name, you should restructure your $team_info array for a faster access with keys being team names and values being team color. We first retrieve all team names using array_column and then same for team_color. We later use array_combine() to make the first array values as keys and second array values as values of those keys. Something like below-

<?php

$team_info = array_combine(array_column($team_info,'team_name'),array_column($team_info,'team_color'));

The above code restructures it from :

Array
(
    [0] => Array
        (
            [team_name] => team1
            [team_color] => red
        )

    [1] => Array
        (
            [team_name] => team2
            [team_color] => blue
        )

    [2] => Array
        (
            [team_name] => team3
            [team_color] => green
        )

    [3] => Array
        (
            [team_name] => team4
            [team_color] => yellow
        )

)

TO

Array
(
    [team1] => red
    [team2] => blue
    [team3] => green
    [team4] => yellow
)

Now, you could just do echo $team_info[$team_cookie] to get the team color.

UPDATE:

If your requirement, is to just search for a single team's color , then you could just use a simple foreach loop and check if the team_cookie matches any team name, then assign it's team_color to a variable.

<?php
$team_color = "";
foreach($team_info as $each_team){
    if($each_team['team_name'] === $team_cookie){
        $team_color = $each_team['team_color'];
        break;
    }
}


echo $team_color;

However, if there are multiple team names to search for, then do as above by restructuring the array. You could also keep them in sessions for better performance time wise.

Upvotes: 2

Ronak Chauhan
Ronak Chauhan

Reputation: 706

Check below code this will help you.

if(isset($_COOKIE['TEAM'])){
    $team_cookie = $_COOKIE['TEAM'];
    $team_info=array (
        '0' => array (
                'team_name' => 'team1',
                'team_color' => 'red'
            ),
        '1' => array (

                'team_name' => 'team2',
                'team_color' => 'blue'
            ),
        '2' => array (

                'team_name' => 'team3',
                'team_color' => 'green'
            ),
        '3' => array(

                'team_name' => 'team4',
                'team_color' => 'yellow'
        )
    );
    $find = check_inarray($team_cookie,$team_info);
    if($find>0){
        //If found do some action
    }


    function check_inarray($team_cookie,$team_info){
        for($i=0;$i<count($team_info);$i++){
            if($team_info[$i]['team_name']==$team_cookie)        
                return true; 
        }
        return false;
    }
}

Upvotes: 0

Related Questions