插入一个包含多个值的数组,其中一些数据库本身就是一个数组

ISSUE

Hello, I have multiple PHP Arrays with multiple values that I am inserting to a SQL database from an XML file. Some of those values are an array on its own (Multi-dimensional array) but the value only gets stored as "Array", none of the sub-values get passed.

EXAMPLE ARRAY

[0] => Array 
 (
  [A] => This is the Title
  [B] => This is the Description
  [C] => Array
      (
         [0] => Value 1
         [1] => Value 2
         [2] => Value 3
       )
    )

I have no problems inserting single values, so A and B will get inserted without a problem. But C will only get inserted as "Array".

INSERTION CODE

  // This is the PHP array that contains multiple items
  $Items 

  // There can be multiple Arrays in this XML so i have to loop through each one
  foreach($Items as $Item => $value) {

    $result = $mysqli->query("INSERT INTO database_name (A, B, C)
                              VALUES ('$value[A]', '$value[B]', '$value[C]');");
   }

I want to serialize the [C] and put it in a single column. But if i do it in the foreach loop like this serialize("$value[C]") only "Array" gets passed to the value. I am a bit lost.

I will appreciate any help I can get.

The easiest and fastest way to serialize (and de-serialize later when needed) is using JSON. Your query would then become:

$result = $mysqli->query("INSERT INTO database_name (A, B, C)
VALUES ('$value[A]', '$value[B]', '" . json_encode($value[C]) . "');");

You should use prepared statement for mysqli and json_encode to serialize values:

foreach($Items as $Item => $value) {
     $stmt = $mysqli->prepare("INSERT INTO database_name (A, B, C)
                              VALUES (? , ?, ?)")
     $stmt->bind_param("sss", json_encode($value[A]), json_encode($value[B]), json_encode($value[C]));

     $stmt->execute();

or if you are sure that only C value is array:

foreach($Items as $Item => $value) {
     $stmt = $mysqli->prepare("INSERT INTO database_name (A, B, C)
                              VALUES (? , ?, ?)")
     $stmt->bind_param("sss", $value[A], $value[B], json_encode($value[C]));

     $stmt->execute();

You are referencing the Array object when call $value[C]

Loop over the [C] to get its contents:

foreach($items as $item) {
    foreach($item as $itemDeep) {
        // do stuff
    }
}

Check each value as an array. If yes, serialize it else use as it is. Check below code, it might help you

// This is the PHP array that contains multiple items
$Items 

// There can be multiple Arrays in this XML so i have to loop through each one
foreach($Items as $Item => $value) {
    $A = is_array($value[A]) ? serialize($value[A]) : $value[A];
    $B = is_array($value[B]) ? serialize($value[B]) : $value[B];
    $C = is_array($value[C]) ? serialize($value[C]) : $value[C];

    $result = $mysqli->query("INSERT INTO database_name (A, B, C)
                            VALUES ('$A', '$B', '$C');");

  $A=$B=$C='';
}

Make empty temporary variables each time to hold new values.