mardi 25 septembre 2018

PHP Array Fill conditional filling regardless of the logic

I have a class which has some functions that build a payload to send to Google Analytics for the measurement protocol. The class has default values in an array and then it has a function that adds onto that array with the arguments passed into the function.

Example:

enter code here

$defaultArrayValues = array(
    'first' = 1,
    'second' = 2,
    'third' = 3);

function generatePayload(hitType, Data1, Data2)
    { 
    if(hitType == 'This')
        {
        $defaultArrayValues['fourth'] => Data1;
        $defaultArrayValues['fifth'] => Data2;
    } else if(hitType == 'That')
        {
        $defaultArrayValues['sixth'] => Data1;
        $defaultArrayValues['seventh'] => Data2; 
    }

generatePayload('This', Data1, Data2);
generatePayload('That', Data3, Data4);

I am expecting the result to have been two different arrays with different data such as:

$defaultArrayValues = array(       
    'first' = 1,
    'second' = 2,
    'third' = 3,
    'fourth' = Data1,
    'fifth' = Data2
);

$defaultArrayValues = array(       
    'first' = 1,
    'second' = 2,
    'third' = 3,
    'sixth' = Data3,
    'seventh' = Data4
);

Instead what I am getting is this:

$defaultArrayValues = array(       
    'first' = 1,
    'second' = 2,
    'third' = 3,
    'fourth' = Data1,
    'fifth' = Data2
);

$defaultArrayValues = array(       
    'first' = 1,
    'second' = 2,
    'third' = 3,
    'fourth' = Data1,
    'fifth' = Data2,
    'sixth' = Data3,
    'seventh' = Data4
);

Where the data is just being added onto the end of the array and not resetting. Im guessing this is something to do with how I am declaring the scope of the functions and variables but I cant figure it out.

Thoughts?

Aucun commentaire:

Enregistrer un commentaire