PHP将lastInsertId()作为数组获取,并添加索引以与另一个数组相同

时间:2016-07-04 18:27:21

标签: php arrays post

我有2个mysql表:facturadetallefactura

factura有两个领域:

id  
cliente

detallefactura有3个字段:

id  
fac_id  
tratamiento  

我希望fac_id成为lastInsertId() factura的{​​{1}}。

我的问题是(id)将有更多的那一行(用户在表单中添加的内容),因此必须是一个数组。

问题是,如何在tratamiento数组中创建多个索引,与fac_id数组相同?现在我明白了:

tratamiento

我需要“另一个索引”Notice: Undefined offset: 1 in C:\xampp\htdocs\noepruebalast\prueba2.php on line 76 DetalleFacturas Object ( [conn:DetalleFacturas:private] => PDO Object ( ) [table_name:DetalleFacturas:private] => detallefactura [id] => [fac_id] => Array ( [0] => 75 ) [tratamiento] => Array ( [0] => Limpieza [1] => Extraer ) )

以下是示例代码:

[1]  => 75

1 个答案:

答案 0 :(得分:0)

这个

  

我只需要一个值(在本例中为75),但我需要将它复制到tratamiento数组这么多次。如果tratamiento是2的数组,我需要[fac_id] =>数组([0] => 75 [1] => 75)

如果你真的需要那么多次,那就做array_keys和array_fill_keys

 $keys = array_keys($tratamiento);
 $fac_id = array_fill_keys( $keys, $insertId );

类似的东西。

http://php.net/manual/en/function.array-keys.php

http://php.net/manual/en/function.array-fill-keys.php

相关问题