Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

GobssRuiz's avatar

How to look up a value within an array with columns/key

I did a database search and it returned some data. I need to create a variable that takes only the team name and id. However, in this data that the bank returns to me, several lines repeat the same time. So I need to check if the time value already exists inside the array. But I'm not able to do it, because I'm creating a value assignment for each array.

I had done it that way, but the id was missing:

if(!in_array($value['title'], $teams)){
        array_push($teams, $value['title']);
}

Now the way it has to be. But my verification is not correct, but I don't know how I can do this. Can anyone help me?

 if(!in_array($value['title'], $teams)){
        array_push($teams, ['title' => $value['title'], 'id' => $value['teams.id']]);
}

Almost complete code:

$teamsusers = TeamsUser::query();
    $teamsusers = $teamsusers->join('users', function($query){
      $query->on('teams_users.user_id', 'users.id')->where('users.status', 'active');
    });
    $teamsusers = $teamsusers->join('teams', function($query){
      $query->on('teams_users.teams_id', 'teams.id')->where('teams.status', 'active');
    });
    $teamsusers = $teamsusers->select('teams_users.teams_id', 'teams_users.user_id', 'users.name', 'users.photo', 'teams.title');
    $teamsusers = $teamsusers->get();

    // Division of what was caught in other variables
    $divisionTimes = [];
    $teams = [];

    foreach ($teamsusers as $key => $value) {
      // Division times
      $divisionTimes[$value['teams_id']]['teamName'] =  $value['title'];

      $divisionTimes[$value['teams_id']]['users'][$key] = [
        'userName' => $value['name'],
        'photo' => $value['photo'],
      ];

      // Just the teams
      if(!in_array($value['title'], $teams)){
        array_push($teams, $value['title']);
      }
    }

Query http://stage.preparadao.com.br.s3.amazonaws.com/time.htm

0 likes
10 replies
vincent15000's avatar

Can you share an example for the array you are handling ?

1 like
vincent15000's avatar

@GobssRuiz This is an Eloquent collection, you can loop through it and retrieve the datas you need.

$users = your collection

foreach($users as $user) {
	$title = $user->title;
	$team_id = $user->team_id;
}

Is that what you want to do ?

GobssRuiz's avatar

@vincent15000 Nop. I'm doing a join at the top of the users and times tables. The teams_users table and the pivot table between these two. In this case, I just want to get the teams, but I cannot repeat it, because in each result, it repeats because there are several users in a single team. So I need to check on the array I add if it already exists. However, I need to save the team name and team id. Did you understand?

1 like
psrz's avatar

So if I understand your code correctly, you want to get all your teams, and for every team the collection/array of every user assigned to that team ? is that correct ?

2 likes
GobssRuiz's avatar

@psrz No, in this case I just need to get the team data. That is, the name of the team and its id. But it can't repeat itself, so I need to do the verification

1 like
psrz's avatar

@GobssRuiz

Then only grab the teams

$teams = Teams::all();

That'll get you all the teams only once.

1 like
psrz's avatar

@GobssRuiz

Why not ? Couldn't be any simpler.

class TeamController  extends Controller
{
    public function index()
    {
        return Teams::all();
    }
}

And you leave the controller you posted above for whatever it was meant for.

1 like

Please or to participate in this conversation.