Mike
Mike

Reputation: 124

PHP Insert value from one array into another

I am having some trouble with a couple of arrays and am stuck. I'm able to read both, but can't figure out how to get the first one to search the second one based on one key and pull the value of another key into the first. Both arrays have code and description. I need to make the description from the next array go into the first where the code is the same. There are multiple instances of code in the first array.

Array1 (
[0] => Array (
    [state] => AK
    [effdate] => 01/01/2012
    [code] => 1
    [description] => null
    [enddate] => null
)
[1] => Array (
    [state] => AK
    [effdate] => 01/01/2012
    [code] => 2
    [description] => null
    [enddate] => null
)
[2] => Array (
    [state] => AK
    [effdate] => 01/01/2012
    [code] => 3
    [description] => null
    [enddate] => null
)
)

Array2 (
[0] => Array (
    [code] => 1
    [description] => some description
)
[1] => Array (
    [code] => 2
    [description] => some other description
)
[2] => Array (
    [code] => 3
    [description] => yet another description
)
)

Also, the first array is being created from a MySQL table while the second array is coming from a tab-delimited text file. The end result will write the descriptions from the text file to the MySQL table. Below is the bit of code that I have to look between the two arrays and write them to a HTML table. I am able to retrieve the description for the very first entry but nothing else. I know it has to do with $i in *$ncci_array[$i]* in the if statement, but I'm not sure what to replace it with. Array1 = *$rates_array* and Array2 = *$ncci_array*

echo '<table border="1"><tr><td>ID</td><td>State</td><td>Effective</td><td>Code</td><td>Description</td><td>End</td></tr>'."\n";

if (($rates_array[$i]['state'] == 'AL'||'AK'||'AZ'||'AR'||'CO'||'CT'||'DC'||'FL'||'GA'||'HI'||'ID'||'IL'||'IA'||'KS'||'KY'||'LA'||'ME'||'MD'||'MS'||'MO'||'MT'||'NE'||'NV'||'NH'||'NM'||'OK'||'RI'||'SC'||'SD'||'TN'||'UT'||'VT'||'VA'||'WI') && ($rates_array[$i]['code'] == $ncci_array[$i]['code']))
{
    $rates_array[$i]['description'] = strtolower($ncci_array[$i]['description']);
}else{
    $rates_array[$i]['description'] = 'Description unavailable at this time';
}

for ($rates_row = 0; $rates_row < count($rates_array)-1; $rates_row++)
{
    if ($rates_array[$i]['code'] == $rates_array[$i+1]['code'])
    {
        $rates_array[$i]['enddate'] = date('Y-m-d', strtotime('-1 day', strtotime($rates_array[$i+1]['effdate'])));
    }else{
        $rates_array[$i]['enddate'] = date('Y-m-d', strtotime('+1 year', strtotime($rates_array[$i]['effdate'])));
    }

    echo '<tr><td>'.$i.'</td><td>'.$rates_array[$i]['state'].'</td><td>'.$rates_array[$i]['effdate'].'</td><td>'.$rates_array[$i]['code'].'</td><td>'.$rates_array[$i]['description'].'</td><td>'.$rates_array[$i]['enddate'].'</td></tr>'."\n";

    $i++;
}
echo '</table>';

The current output looks like this

| ID | State | Effective  | Code | Description      | End        |
| 0  | AK    | 01/01/2011 | 1    | Some description | 12/31/2011 |
| 1  | AK    | 01/01/2012 | 1    |                  | 01/01/2013 |
| 2  | AK    | 01/01/2012 | 2    |                  | 01/01/2013 |
| 3  | AK    | 01/01/2012 | 3    |                  | 01/01/2013 |

Thanks for taking a look!

Upvotes: 0

Views: 1370

Answers (1)

Insyte
Insyte

Reputation: 2236

Loop through both arrays, check for a match between the two. (posted before question edit)

foreach($rates_array as $key => $val){ // Array1 where description is null
    foreach($ncci_array as $k => $v){ // Array2 where description is set
        if($val['code'] == $v['code']) 
            $rates_array[$key]['description'] = $v['description'];
    }
}

This will force the description value from Array2 into Array1 therefore giving you one array to work from in your table to avoid confusion.

Also you don't really need $i as you are using $rates_row as your loop counter.

Upvotes: 1

Related Questions