Why are the terms filled when they are actually empty? Below is the JSON output of a product I am having the issue with:

  {
     "StkGrupKodu1" : "10090",
     "StokAdi" : "7 Parça Kombine Anahtar Takımı",
     "StkGrupKodu2" : "",
     "SiparisVerilebilir" : true,
     "StokMiktari" : 0,
     "StkGrupKodu4" : "",
     "StokTipiAdi" : "Ticari Mallar",
     "KDVOrani" : 18,
     "StokTipi" : "00000",
     "DovizCinsi" : "EUR",
     "Fiyat" : "34.00000",
     "StkGrupKodu3" : "",
     "StkGrupKodu5" : "",
     "PasifStok" : false,
     "EskiBilgileri" : {
        "EskiStokKodu" : "",
        "DegistirmeTarihi" : "1920-04-23T00:00:00"
     },
     "StokKodu" : "JBM50563"
  },


In this e-commerce system, I am consuming a web service and creating corresponding products. With a few products such as the above and a few others, I am having this dreadful issue. As you can see, StkGrupKodu2 and StkGrupKodu3 are empty but when I go to the edit page of the product, it's not empty. You can see from the screenshot below:



This is the code I am using to associate terms with products

foreach ($excel_products as $e) {
    $req = \Drupal::httpClient()-&gt;get("http://mitapp.mitacan.com/b2b/api/vStkLib/GetStkKodlist?UserCode=B2BUSER&amp;StokKodu=".$e-&gt;stokkodu);
    $res = $req-&gt;getBody();
    $res = json_decode($res);
    $data = $res-&gt;Data[0];
    $sid = \Drupal::database()-&gt;select("commerce_store_field_data", "csfd")-&gt;fields("csfd", ["store_id"])-&gt;condition("default_currency", ($data-&gt;DovizCinsi == "TRL")?"TRY":$data-&gt;DovizCinsi)-&gt;execute()-&gt;fetchCol();
    $product = Product::create(["type" =&gt; "default"]);
    &lt;snip&gt;
    if (!empty($data-&gt;StkGrupKodu1)) {
        $tid_1 = \Drupal::database()-&gt;select("taxonomy_term__field_stok_grup_kodu", "ttfsgk")-&gt;fields("ttfsgk", ["entity_id"])-&gt;condition("field_stok_grup_kodu_value", $data-&gt;StkGrupKodu1)-&gt;execute()-&gt;fetchCol();
        $tid_1 = (!empty($tid_1[0]))?$tid_1[0]:"";
    }
    if (!empty($data-&gt;StkGrupKodu2)) {
        $tid_2 = \Drupal::database()-&gt;select("taxonomy_term__field_stok_grup_kodu", "ttfsgk")-&gt;fields("ttfsgk", ["entity_id"])-&gt;condition("field_stok_grup_kodu_value", $data-&gt;StkGrupKodu2)-&gt;execute()-&gt;fetchCol();
        $tid_2 = (!empty($tid_2[0]))?$tid_2[0]:"";
    }
    if (!empty($data-&gt;StkGrupKodu3)) {
        $tid_3 = \Drupal::database()-&gt;select("taxonomy_term__field_stok_grup_kodu", "ttfsgk")-&gt;fields("ttfsgk", ["entity_id"])-&gt;condition("field_stok_grup_kodu_value", $data-&gt;StkGrupKodu3)-&gt;execute()-&gt;fetchCol();
        $tid_3 = (!empty($tid_3[0]))?$tid_3[0]:"";
    }
    $tids = [$tid_1, $tid_2, $tid_3];
    $tids = array_filter($tids);
    &lt;snip&gt;
    foreach ($tids as $tid) {
        $product-&gt;field_stok_grup_adi[] = [
            "target_id" =&gt; $tid,
        ];
    }
  &lt;snip&gt;
}


What am I doing wrong or missing?
Looks to be a PHP issue and not drupal. You're not clearing your $tid_1,$tid_2,$tid_3 values within your for loop

So when $data->StkGrupKodu1 isn't set for example the value of $tid_1 is still that of the last time $data->StkGrupKodu1 did exist.

You could declare your $tid_1,$tid_2,$tid_3 variables at the top of your foreach loop so they get set similar to what you're doing if the database results null