0

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.

1
  • 1
    Easiest way? json_encode it. Commented May 16, 2016 at 13:54

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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]) . "');");

1 Comment

This was perfect, simple and it works. I will accept your answer in 5 minutes.
1

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();

Comments

1

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
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.