Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2003
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Loop through 2 associative arrays

    Coders,
    I'm struggling with 2 arrays that I need to combine the values of where the keys match.

    The first array I am building with the code:
    PHP Code:
    $vcdata[] = array( trim($row[0]) => trim($row[1]) ); 
    This outputs (just a sample):
    Code:
    Array
    (
        [0] => Array
            (
                [SiteID] => 00000
                [VCTotal] => 0
            )
    
        [1] => Array
            (
                [SiteID] => 00001
                [VCTotal] => 0
            )
    
        [2] => Array
            (
                [SiteID] => 00002
                [VCTotal] => 2
            )
    )
    The second array is my "lookup" table so to speak and is built with:
    PHP Code:
    $ca_sites[] = array(trim(odbc_result($result"SiteID")) => trim(odbc_result($result"SiteName"))); 
    This outputs (just a sample):
    Code:
    Array
    (
        [0] => Array
            (
                [00011] => Waco 06
            )
    
        [1] => Array
            (
                [00016] => Burleson
            )
    
        [2] => Array
            (
                [00024] => Austin 27
            )
    )
    I would like to loop through the first array (vcdata) to "extract" only the items where the key is present in my second array (ca_sites). I'd like to keep the value of both keys and build a new array that has only the key and value from ca_sites, and an array that has only the values from the vcdata array.

    This was my attempt:
    PHP Code:
    foreach ($vcdata as $okey => $ovalue) {
             echo 
    $okey.'=>'.$ovalue.'<br />';
            foreach (
    $ca_sites as $ikey => $ivalue) {
                if(
    $okey==$ikey){
                    
    $sites_final[] = $ivalue["SiteID"] . ' - ' $ivalue[1];
                    
    $totals_final[] = $ovalue;
                }
            }
        } 
    but the output is not what is expected. It prints the word "Array" on the echo line. What am I missing?
    i'm always learning

  • #2
    New Coder
    Join Date
    Nov 2003
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I kinda already made that conclusion, but any time I would try to directly reference an array item, it would be blank.

    A var_dump on the array itself (like ca_sites) would show everything, but when I would do something like $ca_sites[$i][0], it would return nothing.

    I just need to loop through the bigger array (vcdata) and if the "key" appears in the "ca_sites" array as a key, I'd like to store both "values" and just one of the keys in a new array.

    I've managed to get a new array with the key and only one of the values, but I need both and not sure how to go about doing that because my referencing of the 2D array isn't right.
    i'm always learning

  • #3
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    katalyst, is this the code you were looking for?

    PHP Code:
    foreach ($vcdata as $vcKey => $vcArray) {
        foreach (
    $ca_sites as $caKey => $caArray) {
            if (
    array_key_exists($vcArray['SiteID'], $caArray)) {
                
    $sites_final[] = $vcArray['SiteID'].' - '.$vcArray['VCTotal'];
                
    $totals_final[] = $caArray[$vcArray['SiteID']];
            }
        }

    If it is, I will help explain what it is doing, if you would like me to.

  • #4
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    Well, the post above was fixing your original code. But there's an easier way to do what you're wanting. Instead of making your array two-dimensional, let's just keep it simple and use the SiteID as the key and the VCTotal as the value. Like so:

    PHP Code:
    $vcdata[trim($row[0])] = trim($row[1]);

    $ca_sites[trim(odbc_result($result"SiteID"))] = trim(odbc_result($result"SiteName")); 
    Then, the foreach becomes easier as well:

    PHP Code:
    foreach ($vcdata as $SiteID => $VCTotal) {
        if (
    array_key_exists($SiteID$ca_sites)) {
            
    $sites_final[] = $SiteID.' - '.$VCTotal;
            
    $totals_final[] = $ca_sites[$SiteID];
        }

    Does this make sense? Does it do what you're wanting?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •