使用PHP (laravel)时,如果数组元素未定义,则向数据库插入null


insert null to database if array element is undefined using php (laravel)

我试图插入一个数组到一个表,但数组的一些元素没有定义。我想在元素未定义的地方插入null。数组如下:

[0]=>Array
 (
   [0] => Array
    (
        [Id] => 120
        [Title] => PHP
        [Year] => 2011
        [Version] =>5.5
    )
   [1] => Array
    (
        [Id] => 121
        [Title] => Javascript
        [Year] => 2010
        [Version] =>7
    )
 )
 [1]=>Array
 (
   [0] => Array
    (
        [Id] => 1
        [Title] => Html
        [Author] => peter
    )
   [1] => Array
    (
        [Id] => 1
        [Title] => Asp
        [Author] => john
    )
 )
  [2]=>Array
 (
   [0] => Array
    (
        [Id] => 1
        [Title] => Html
        [Year] => 2011
        [Page] =>40
    )
   [1] => Array
    (
        [Id] => 1
        [Title] => Asp
        [Year] => 2010
        [Page] =>220
    )
 )

表有6个字段:id、标题、年份、作者、版本和页面。

 $this ->table->create(array(
    'id'       => $data['Id'] ,
   'title'     => $data['Title'] ,
   'year'      => $data['Year'] ,
   'author'    => $data['Author'],
   'version'   => $data['Version'] ,
   'page'      => $data['Page'] ,
   ))

如何在变量未定义时向表插入null。比如未定义($ data['版本'])?null: $data['Version'] !!!!!!一个PHP函数或任何可能的解决方案?由于

你可能会:

foreach($data as $row)
{
    $info = array(
       'id'       => $this->getData($row, 'Id'),
       'title'     => $this->getData($row, 'Title'),
       'year'      => $this->getData($row, 'Year'),
       'author'    => $this->getData($row, 'Author'),
       'version'   => $this->getData($row, 'Version'),
       'page'      => $this->getData($row, 'Page'),
    );
    $this->table->create(array_filter($info));
}
public function getData($data, $key)
{
    return isset($data[key]) ? $data[key] : null;
}