由于数组数据类型错误,无法插入mysql表

Following is the array from which the data are retrieved and tried to be inserted into mysql table but works well when more than 1 row to be inserted and fails when only 1 row to be inserted.

// The structure of the array is as follows
Array
(
    [Row] => Array
        (
            [0] => Array
                (
                    [PIN] => 1274
                    [DateTime] => 2018-04-07 09:28:16
                    [Verified] => 15
                    [Status] => 3
                    [WorkCode] => 0
                )

            [1] => Array
                (
                    [PIN] => 157
                    [DateTime] => 2018-04-07 10:22:56
                    [Verified] => 15
                    [Status] => 3
                    [WorkCode] => 0
                )
    // these are the raw punch data from biometric machine

Following is the piece of code which currently being used.

  if(isset($array_att_logs) && isset($array_att_logs['Row'])) {     
      foreach ($array_att_logs['Row'] as $value) {
          $emp_code =  $value['PIN'];       // line 92                                  $dateNTime =  $value['DateTime']; //line 93

$punch_query = "INSERT IGNORE INTO punching_data_table (emp_code, date_time,in_out_status) VALUES ('$emp_code', '$dateNTime','$in_out_status')";

 $punch_result = mysql_query($punch_query);
 echo mysql_error();
         }       
     } echo var_dump($array_att_logs);

Please note that the $in_out_status is defined explicitly elsewhere which does not seem to have any problem at all.I have checked using var_dump() that the array exists with data just fine. The problem is if there is a single row to be inserted into the table then following error is shown.

Warning: Illegal string offset 'PIN' in /home1/.../punch-raw.php on line 92
Warning: Illegal string offset 'DateTime' in /home1/..../punch-raw.php on line 93

If there is more than 1 row to be inserted into the table then it works just fine. May be I am defining $emp_code and $dateNTime incorrectly.

Here is how it looks like when there is a single row.

 echo " <pre>"; 
    print_r($array_att_logs); 
    echo "/<pre>"; 

Array
(
    [Row] => Array
        (
            [PIN] => 406
            [DateTime] => 2018-05-06 14:40:09
            [Verified] => 1
            [Status] => 3
            [WorkCode] => 0
        )

)
/

using var_dump it looks like below

array(1) { ["Row"]=> array(5) { ["PIN"]=> string(3) "406" ["DateTime"]=> string(19) "2018-05-06 14:40:09" ["Verified"]=> string(1) "1" ["Status"]=> string(1) "3" ["WorkCode"]=> string(1) "0" } }

</div>

The reason that you are having the problem is that the array structure of $array_att_logs is different when there is only 1 row to when there are more than one row. Thus you need to test for that situation and change the way you iterate through the array. The easiest way to check if there is only 1 row is to see if the contents of $array_att_logs['Row'] is an array of arrays. To do this we use array_values($array_att_logs['Row']) to ensure that we have an entry with an index of 0, and then test that value to see if it is an array. Then we use that result to change the value being iterated by the foreach:

$row = array_values($array_att_logs['Row']);
$multirows = is_array($row[0]);
foreach (($multirows ? $array_att_logs['Row'] : $array_att_logs) as $value) {
    $emp_code =  $value['PIN'];
    ...