phparraysfor-looparray-combine

How do I add variable numbers of key value pairs using PHP array_combine and for-loop?


I have a db in which I store 20 demographics. The demographics come from a csv and are stored in array. Each array key is a placeholder: d1, d2, up to d20. The incoming data may be [race, gender, class] for Client A and [income, region] for Client B.

For both clients the db table will store their values as d1, d2, d3...d20. The data are associated with a clientid upon insertion into the database.

I need to write a single insert statement for the database table. I need it to include d1...d20. There will always be 20 placeholders. Some are filled, some are not like int he example above.

The challenge is that I don't know how many will be filled.

So, I create an array of keys. I combine this with an array of incoming csv data.

$demos['d01']='';
$demos['d02']='';
$demos['d03']='';
$demos['d04']='';
...
$demos['d20']='';

And I produce this result.

print_r($rowdata);

    [1] => Array
        (
            [d01] => 1
            [firstname] => Fred
            [lastname] => Dryer
            [email] => FredDryer1@email.com
            [d02] => Backfield
            [d03] => North
            [partnerid] => 14
            [d04] =>
            ...
            [d20] => 
        )

In fact, this is what I want. But, I can only seem to get there MANUALLY by adding $rowdata_tmp['d04'] = ''; to match the addition of d04 to $demos_keys. When the number of keys doesn't match the number of elements in the second array, the code fails. See below.

I need to accomplish this match programmatically.

The solution I need is the for-loop below. I set it to start at 4 here for demonstration purposes because in this case I know that's where it needs to start.

I have tried $x = $place_to_start where $place_to_start = 4 derived through maths relative to the incoming the csvdata.

But, no joy. The loop doesn't work. 500 error. No help!

$place_to_start = 4;
foreach ($csvdata as $row) {
    $rowdata_tmp = explode(',', trim($row));
    $rowdata_tmp['partnerid'] = $partnerid;

    $rowdata_tmp['d04'] = ''; 

        // for ($x = 4; $x = 20; $x++) {
            // if ($x <10) {
                // $rowdata_tmp['d0'.$x] = '';
            // } else {
                // $rowdata_tmp['d'.$x] = '';   
            // }
        // }
        
    $rowdata[] = array_combine($demos_keys, $rowdata_tmp);

}

Anyone see my problem as it relates to the code? I understand there may have been more wise design choices made in the past...here's where we are. All comments are mucho apprieciado.


Solution


  • This part for ($x = 4; $x = 20; $x++) { will cause an infinite loop. To include 20 in the result, you can use

    for ($x = 4; $x <= 20; $x++) {
    

    What you might do is subtract the number of items in $rowdata_tmp from 20 and set the start of the for loop to the amount of $rowdata_tmp assuming that the keys from the csv are not the same.

    $rowdata_tmp = explode(',', trim($row));
    $nrItemsFromCsv = count($rowdata_tmp);
    $nrItemsToAdd = 20 - $nrItemsFromCsv;
    
    for ($x = $nrItemsFromCsv; $x <= $nrItemsToAdd; $x++) {
        if ($x < 10) {
            $rowdata_tmp['d0' . $x] = '';
        } else {
            $rowdata_tmp['d' . $x] = '';
        }
    }
    
    print_r($rowdata_tmp);