Master Array compare csv

63 views Asked by At

I have two array( master and respondent). I want to key match them then write in csv following the master as a header. The array looked like this. array (size=134)

this is the master
  0 => string 'tfa_1' (length=5)
  1 => string 'tfa_10' (length=6)
  2 => string 'tfa_11' (length=6)
  3 => string 'tfa_12' (length=6)
  4 => string 'tfa_3' (length=5)
  5 => string 'tfa_127' (length=7)
  6 => string 'tfa_150' (length=7)
  7 => string 'tfa_156' (length=7)
  8 => string 'tfa_157' (length=7)
  9 => string 'tfa_158' (length=7)
  10 => string 'tfa_128' (length=7)
  11 => string 'tfa_137' (length=7)
  12 => string 'tfa_131' (length=7)
  13 => string 'tfa_138' (length=7)
  14 => string 'tfa_139' (length=7)
  15 => string 'tfa_225' (length=7)
  16 => string 'tfa_226' (length=7)
  17 => string 'tfa_227' (length=7)
  18 => string 'tfa_228' (length=7)
  19 => string 'tfa_229' (length=7)
  20 => string 'tfa_230' (length=7)
  21 => string 'tfa_231' (length=7)
  22 => string 'tfa_232' (length=7)
  23 => string 'tfa_233' (length=7)
  24 => string 'tfa_234' (length=7)
  25 => string 'tfa_235' (length=7)
  26 => string 'tfa_236' (length=7)
  27 => string 'tfa_331' (length=7)
  28 => string 'tfa_333' (length=7)
  29 => string 'tfa_239' (length=7)
  30 => string 'tfa_338' (length=7)
  31 => string 'tfa_339' (length=7)
  32 => string 'tfa_242' (length=7)
  33 => string 'tfa_243' (length=7)
  34 => string 'tfa_305' (length=7)
  35 => string 'tfa_245' (length=7)
  36 => string 'tfa_246' (length=7)
  37 => string 'tfa_247' (length=7)
  38 => string 'tfa_350' (length=7)
  39 => string 'tfa_351' (length=7)
  40 => string 'tfa_250' (length=7)
  41 => string 'tfa_356' (length=7)
  42 => string 'tfa_357' (length=7)
  43 => string 'tfa_253' (length=7)
  44 => string 'tfa_344' (length=7)
  45 => string 'tfa_345' (length=7)
  46 => string 'tfa_256' (length=7)
  47 => string 'tfa_257' (length=7)
  48 => string 'tfa_374' (length=7)
  49 => string 'tfa_375' (length=7)
  50 => string 'tfa_376' (length=7)
  51 => string 'tfa_377' (length=7)
  52 => string 'tfa_378' (length=7)
  53 => string 'tfa_379' (length=7)
  54 => string 'tfa_380' (length=7)
  55 => string 'tfa_381' (length=7)
  56 => string 'tfa_382' (length=7)
  57 => string 'tfa_383' (length=7)
  58 => string 'tfa_384' (length=7)
  59 => string 'tfa_385' (length=7)
  60 => string 'tfa_386' (length=7)
  61 => string 'tfa_387' (length=7)
  62 => string 'tfa_272' (length=7)
  63 => string 'tfa_396' (length=7)
  64 => string 'tfa_397' (length=7)
  65 => string 'tfa_398' (length=7)
  66 => string 'tfa_399' (length=7)
  67 => string 'tfa_400' (length=7)
  68 => string 'tfa_401' (length=7)
  69 => string 'tfa_279' (length=7)
  70 => string 'tfa_407' (length=7)
  71 => string 'tfa_408' (length=7)
  72 => string 'tfa_409' (length=7)
  73 => string 'tfa_283' (length=7)
  74 => string 'tfa_416' (length=7)
  75 => string 'tfa_417' (length=7)
  76 => string 'tfa_418' (length=7)
  77 => string 'tfa_419' (length=7)
  78 => string 'tfa_496' (length=7)
  79 => string 'tfa_497' (length=7)
  80 => string 'tfa_498' (length=7)
  81 => string 'tfa_499' (length=7)
  82 => string 'tfa_502' (length=7)
  83 => string 'tfa_503' (length=7)
  84 => string 'tfa_504' (length=7)
  85 => string 'tfa_505' (length=7)
  86 => string 'tfa_506' (length=7)
  87 => string 'tfa_507' (length=7)
  88 => string 'tfa_509' (length=7)
  89 => string 'tfa_511' (length=7)
  90 => string 'tfa_512' (length=7)
  91 => string 'tfa_514' (length=7)
  92 => string 'tfa_515' (length=7)
  93 => string 'tfa_516' (length=7)
  94 => string 'tfa_518' (length=7)
  95 => string 'tfa_519' (length=7)
  96 => string 'tfa_520' (length=7)
  97 => string 'tfa_521' (length=7)
  98 => string 'tfa_526' (length=7)
  99 => string 'tfa_528' (length=7)
  100 => string 'tfa_529' (length=7)
  101 => string 'tfa_535' (length=7)
  102 => string 'tfa_537' (length=7)
  103 => string 'tfa_538' (length=7)
  104 => string 'tfa_539' (length=7)
  105 => string 'tfa_540' (length=7)
  106 => string 'tfa_541' (length=7)
  107 => string 'tfa_542' (length=7)
  108 => string 'tfa_543' (length=7)
  109 => string 'tfa_544' (length=7)
  110 => string 'tfa_545' (length=7)
  111 => string 'tfa_546' (length=7)
  112 => string 'tfa_547' (length=7)
  113 => string 'tfa_548' (length=7)
  114 => string 'tfa_549' (length=7)
  115 => string 'tfa_550' (length=7)
  116 => string 'tfa_551' (length=7)
  117 => string 'tfa_553' (length=7)
  118 => string 'tfa_554' (length=7)
  119 => string 'tfa_555' (length=7)
  120 => string 'tfa_556' (length=7)
  121 => string 'tfa_557' (length=7)
  122 => string 'tfa_558' (length=7)
  123 => string 'tfa_559' (length=7)
  124 => string 'tfa_561' (length=7)
  125 => string 'tfa_562' (length=7)
  126 => string 'tfa_563' (length=7)
  127 => string 'tfa_564' (length=7)
  more elements...

this is the respondent array

 array (size=19)
  0 => string 'tfa_1' (length=5)
  1 => string 'tfa_10' (length=6)
  2 => string 'tfa_3' (length=5)
  3 => string 'tfa_127' (length=7)
  4 => string 'tfa_150' (length=7)
  5 => string 'tfa_156' (length=7)
  6 => string 'tfa_128' (length=7)
  7 => string 'tfa_502' (length=7)
  8 => string 'tfa_528' (length=7)
  9 => string 'tfa_529' (length=7)
  10 => string 'tfa_146' (length=7)
  11 => string 'tfa_dbCounters' (length=14)
  12 => string 'tfa_dbFormId' (length=12)
  13 => string 'tfa_dbResponseId' (length=16)
  14 => string 'tfa_dbControl' (length=13)
  15 => string 'tfa_dbTimeStarted' (length=17)
  16 => string 'tfa_referrer' (length=12)
  17 => string 'tfa_dbVersionId' (length=15)
  18 => string 'tfa_switchedoff' (length=15)

I don't know how to put them in a csv that follows the master as a header and the respondent to be below the master array corresponding the same string and ignore the array content that don't have a pair. Please help I'm desperate.

1

There are 1 answers

0
liu jin long On

I used for loop

$data=array();  
          foreach($formHeader as $key=>$value){
            if (array_key_exists($key, $normalized_array)) {
                $data[$key]=$normalized_array[$key];
          }
          else{
            $data[$key]="";

        }   

      }