PHP,cURL,REST-API - 将具有许多值的变量放入JSON数组中

with the following code

<?php
                //PHP Infos anzeigen lassen
            //phpinfo();
          header('Content-type: text/html; charset=utf-8');
            // Inhaltstyp und Zeichenkodierung für Skript festlegen
                $User_Agent = 'Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0';

            $id = $_POST["id"];             
                // Übernahme Formulareingabe
                $url = "http://hub.culturegraph.org/entityfacts/$id";
                // URL festlegen

                $request_headers[] = 'Accept: application/json';
              $request_headers[] = 'Content-Type: application/json; charset=utf-8';
              $request_headers[] = 'Accept-Encoding:    gzip, deflate, identity';
              $request_headers[] = 'Accept-Language: de,en-US;q=0.7,en;q=0.3';
              $request_headers[] = 'X-picturemaxx-api-key: key';
                $request_headers[] = "Authorization: Bearer token";
            // Optionale Anfrageoptimierungen

              $ch = curl_init($url);
              //  Initiate curl
        curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
                curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_ENCODING, "");
                // Anfrage optimieren
                // Execute
                $result = curl_exec($ch); // Performs the Request, with specified curl_setopt() options (if any).
        curl_close($ch);
        // Closing

        $data = json_decode($result, true); // Dekodiert eine JSON-Zeichenkette, Übergibt an Variable
        foreach($data['variantName'] as $alternativ) {
        echo $alternativ . " ; ";

}

i receive json-data. The output from the variable $alternativ looks like

"Johann Wolfgang Goethe ; Johan Wolfgang von Goethe ; Johan Wolphgang Goethe ; Johan W. von Goethe ; Joh. Wolfg. v. Goethe ; J. Wolfgang Goethe ; J. W. v. Goethe ; J. W. Goethe ; Jan Wolfgang Goethe ; Jean Wolfgang von Goethe ; Juan Wolfgang von Goethe ; Juan Wolfgang Goethe ; Juan W. Goethe ; João Wolfgang von Goethe ; Iohann Wolfgang Goethe ; Iohan Wolphgang Goethe ; Ioannes W. Goethe ; I. W. Goethe ; Wolfgango Goethe ; Wolfango Goethe ; W. von Goethe ; Volfgango Goethe ; Volfango Goethe ; Giov. L. Goethe ; G. L. Goethe ; Goethe ;

and so on.

Next i try is to PUT the values of the variable $alternativ into the json-array/database field 'classification_element_name'

$url2 = "https://bpk.bs.picturemaxx.com/api/v1/editing/classifications/42/elements/2156013";



$dataj = array (
  'classification_element_parent_id' => 0,
  'classification_element_matchcode' => '',
  'classification_element_foreignref' => '',
  'localized' => 
  array (
    'en-us' => 
    array (
      'classification_element_name' => '',
    ),
    'de-de' => 
    array (
      'classification_element_name' => $alternativ,
    ),
  ),
);

        $data_json = json_encode($dataj);

              $ch = curl_init($url2);
              // Set the url
        curl_setopt( $ch, CURLOPT_URL, $url2 );
        curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
                curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
        curl_setopt($ch, CURLOPT_ENCODING, "");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);

                // Execute
                $result2 = curl_exec($ch); // Performs the Request, with specified curl_setopt() options (if any).
                $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                // Closing
        curl_close($ch);

This works. But only one respectively the last value of the originally many values from the variable $alternativ appears in the database field. How do I change the code so that all values of the variable $alternativ are transferred into the database field?

you just loop & echo the elements and dont assign them to a variable

foreach($data['variantName'] as $alternativ) {
    echo $alternativ . " ; ";
}

$alternativ will allways be the last element of $data['variantName']. You need to replace $alternativ with $data['variantName'] in your $dataj array or assign a new Variable and use this in your $dataj array

eg:

$alternativData = array();
foreach($data['variantName'] as $alternativ) {
    echo $alternativ . " ; ";
    $alternativData[] = $alternativ;
}

$dataj = array (
  'classification_element_parent_id' => 0,
  'classification_element_matchcode' => '',
  'classification_element_foreignref' => '',
  'localized' => 
  array (
    'en-us' => 
    array (
      'classification_element_name' => '',
    ),
    'de-de' => 
    array (
      'classification_element_name' => $alternativData,
    ),
  ),
);

or if it should look like 'data1;data2;data3..' them use:

$alternativData = array();
foreach($data['variantName'] as $alternativ) {
    echo $alternativ . " ; ";
    $alternativData[] = $alternativ;
}

$dataj = array (
  'classification_element_parent_id' => 0,
  'classification_element_matchcode' => '',
  'classification_element_foreignref' => '',
  'localized' => 
  array (
    'en-us' => 
    array (
      'classification_element_name' => '',
    ),
    'de-de' => 
    array (
      'classification_element_name' => implode(';', $alternativData),
    ),
  ),
);