-1

I am simply trying to add a set of keys and values to an array if and only if they are not already in the array. I need to add d1 thru d20 when they are not already in the array,

Here is my code.

print_r($demos_keys)

Array
(
    [0] => d01
    [1] => firstname
    [2] => lastname
    [3] => email
    [4] => d02
    [5] => d03
    [6] => partnerid
)
$counter=0;
foreach ($csvdata as $row) {
    if ($counter > 0) {
    $rowdata_tmp = explode(',', trim($row));
    $rowdata_tmp['partnerid'] = $partnerid;
    $rowdata[] = array_combine($demos_keys, $rowdata_tmp);


//Fails to add 'd04'
        // if(isset($rowdata['d04'])){
        // $x=1;
        // } else {
            // $rowdata['d04']='';
        // }

//Fails to add 'd04'
        // if(isset($rowdata['d04'])){
        // $x=1;
        // } else {
            // $row['d04']='';
        // }
        
//Fails 
    if (array_key_exists('d04',$rowdata)==FALSE) {$rowdata['d04'] = '';}
    }

//Fails 
    if (array_key_exists('d04',$rowdata)==FALSE) {$row['d04'] = '';}
    }


    $counter = $counter + 1;
    
}

print_r($rowdata);

Array
(
    [0] => Array
        (
            [d01] => 1
            [firstname] => Fred
            [lastname] => Dryer
            [email] => [email protected]
            [d02] => Backfield
            [d03] => North
            [partnerid] => 14
        )

I CAN add d04 by inserting $rowdata_tmp['d04'] = ''; before the array_combine statement, but the problem is that d04 will sometimes already be present in demos_keys.

Can someone help? I'm dying here.

8
  • 1
    Please clarify the question. Also could you please add a minimal reproducible example! Commented Feb 14, 2021 at 16:42
  • Do you really want to create a 2-dimensional array? $rowdata[] = is creating a new row in the array, but then you're checking for the key in the top-level array, not the row. Commented Feb 14, 2021 at 16:44
  • I suspect that should just be $rowdata = Commented Feb 14, 2021 at 16:45
  • Can you clarify which array you want to modify when? Initially, you dump $demos_keys, afterwards you use $rowdata and $rowdata_tmp and $row Commented Feb 14, 2021 at 16:46
  • Also, what does "fails to add" mean? Is there any error message given, or is anything just not working as expected? Commented Feb 14, 2021 at 16:47

1 Answer 1

1

You're checking for the key in the top-level $rowdata array, not the new row that you're adding to the array.

$rowdata_tmp = explode(',', trim($row));
$rowdata_tmp[] = $partnerid;
$new_row = array_combine($demos_keys, $rowdata_tmp);
if (isset($new_row['d04'])) {
    $x = 1;
} else {
    $new_row['d04'] = '';
}
$rowdata[] = $new_row;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Barmar. Still learning here. Much appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.