1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2017
    Rep Power

    Checking for values in associative array

    Checking for values in associative array
    I have 2 associative arrays for 2 separate queries to a database. The first associative array gives me the ID and names of all technicians from the table Techs. The other array gives me the ID and names of the technicians assigned to a particular issue. When I loop through the first array, how would I check to see if that technician is assigned to this issue?

    Here are my arrays:

    AllTechs CurrentTechs
    ID Name ID Name
    1 John Doe 1 John Doe
    2 Jim Deen 4 Jack Sprat
    3 Jane Ploof
    4 Jack Sprat

    So in wrapping up, I am looping through the AllTechs array and I want to know if the row I'm on is also in the CurrentTechs array. I know how to do it with a 1-dimensional array using in_array, but not sure how to do it in this instance. Would it be easier to just loop through CurrentTechs to look for the ID/Name?
  2. #2
  3. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Caro, Michigan
    Rep Power
    Kind of depends on the structure of your arrays. I mean, this works:

    $all_techs = array('12' => 'John Doe', '19' => 'Jim Dean', '35' => 'Jane Something', '88' => 'Jimbo Fishhead');
    $current_techs = array('19','88');
    foreach($all_techs as $key => $value) {
        if(in_array($key, $current_techs)) {
            echo "{$value} is assigned to the issue\n";
        } else {
            echo "{$value} is not assigned to this issue\n";
    John Doe is not assigned to this issue
    Jim Dean is assigned to the issue
    Jane Something is not assigned to this issue
    Jimbo Fishhead is assigned to the issue
    You don't need names in the second "current issue" tech array. You already have names in the first. Use the IDs as keys. This would work even if you had first name, last name, etc. as separate arrays under each ID.

    You may even be able to do this all in the query with a proper join, depending on what you're overall goal is.

    -- Cigars, whiskey and wild, wild women. --

IMN logo majestic logo threadwatch logo seochat tools logo