【问题标题】:Need help how to achieve this multiple array to be inserted to database in Php Laravel需要帮助如何在 Php Laravel 中实现这个要插入数据库的多个数组
【发布时间】:2019-11-22 19:51:31
【问题描述】:

我想将 XML 数据导入我的数据库。但是我很难循环它,因为数组不是同一层。

我试过foreach,但有些数据没有插入,虽然每个数据的键是一样的。

这是当前的输出数组

array:3 [▼
  0 => array:4 [▼
    "BANFN" => "001"
    "BNFPO" => "00010"
    "LIFNR_ID" => "0000001244"
    "STATUS" => array:2 [▼
      "MSGTYP" => "S"
      "MSG" => "random text"
    ]
  ]
  1 => array:4 [▼
    "BANFN" => "0011023036"
    "BNFPO" => "00010"
    "LIFNR_ID" => "0000001244"
    "STATUS" => array:2 [▼
      "MSGTYP" => "S"
      "MSG" => "random text"
    ]
  ]
  2 => array:2 [▼
    0 => array:4 [▼
      "BANFN" => "0011022474"
      "BNFPO" => "00010"
      "LIFNR_ID" => "0000003468"
      "STATUS" => array:2 [▼
        "MSGTYP" => "E"
        "MSG" => "random text"
      ]
    ]
    1 => array:4 [▼
      "BANFN" => "0011022474"
      "BNFPO" => "00010"
      "LIFNR_ID" => "0000003468"
      "STATUS" => array:2 [▼
        "MSGTYP" => "S"
        "MSG" => "random text"
      ]
    ]
  ]
]

这是我想要实现的代码

array:3 [▼
  0 => array:4 [▼
    "BANFN" => "0011023037"
    "BNFPO" => "00010"
    "LIFNR_ID" => "0000001244"
    "STATUS" => array:2 [▼
      "MSGTYP" => "S"
      "MSG" => "random text"
    ]
  ]
  1 => array:4 [▼
    "BANFN" => "0011023036"
    "BNFPO" => "00010"
    "LIFNR_ID" => "0000001244"
    "STATUS" => array:2 [▼
      "MSGTYP" => "S"
      "MSG" => "random text"
    ]
  ]
2 => array:4 [▼
    "BANFN" => "0011023036"
    "BNFPO" => "00010"
    "LIFNR_ID" => "0000001244"
    "STATUS" => array:2 [▼
      "MSGTYP" => "S"
      "MSG" => "random text"
    ]
  ]
3 => array:4 [▼
    "BANFN" => "0011023036"
    "BNFPO" => "00010"
    "LIFNR_ID" => "0000001244"
    "STATUS" => array:2 [▼
      "MSGTYP" => "S"
      "MSG" => "random text"
    ]
  ]
4 => array:4 [▼
    "BANFN" => "0011023036"
    "BNFPO" => "00010"
    "LIFNR_ID" => "0000001244"
    "STATUS" => array:2 [▼
      "MSGTYP" => "S"
      "MSG" => "random text"
    ]
  ]
]

我需要用键将同一层上的数组展平。但我想不通。或者是否有任何用于将所有数组插入数据库的 foreach 或循环方法?

创建原始数组的代码是

$url = Storage::disk('ftp')->allFiles('PM/PR_RFQNUM/'); 
$v = []; 
foreach ($url as $key => $files) { 
    //$file[] = $files; 
    $xml = Storage::disk('ftp')->get($files); 
    $file = Formatter::make($xml, Formatter::XML)->toArray(); 
    foreach($file as $k=>$value){ 
        $v[] = $value; 
    } 
}

【问题讨论】:

  • 你如何得到你当前的数组,你处理的数据是什么?
  • 这是我的 foreach $url = Storage::disk('ftp')->allFiles('PM/PR_RFQNUM/'); $v = []; foreach ($url as $key => $files) { //$file[] = $files; $xml = Storage::disk('ftp')->get($files); $file = Formatter::make($xml, Formatter::XML)->toArray(); foreach($file as $k=>$value){ $v[] = $value; } }' @NigelRen

标签: php arrays laravel


【解决方案1】:

此循环将数组展平并将记录保存到数据库中:

foreach ($file as $value){ 
    $item = new SomeModel();
    $item->BANFN = $value['BANFN'];
    $item->BNFPO = $value['BNFPO'];
    $item->LIFNR_ID = $value['LIFNR_ID'];
    $item->STATUS_MSGTYP = $value['STATUS']['MSGTYP'];
    $item->STATUS_MSG = $value['STATUS']['MSG'];
    $item->save();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-31
    • 1970-01-01
    相关资源
    最近更新 更多