重组数组有问题。
我正在使用批量上传数据(更新)到数据库。所以我有一个像这样的3阵列。
Array ( [0] => aaa [1] => ccc [2] => eee [3] => ggg )
Array ( [0] => bbb [1] => ddd [2] => fff [3] => hhh )
Array ( [0] => 1662 [1] => 1663 [2] => 1664 [3] => 1665 )
所以我想要的是,我需要将数组重组为下面的给定顺序。然后上传(更新)到数据库。
$data = array(
array(
'title' => 'aaa' ,
'Info' => 'bbb' ,
'id' => '1662'
),
array(
'title' => 'ccc' ,
'Info' => 'ddd' ,
'id' => '1663'
),
array(
'title' => 'eee' ,
'Info' => 'fff' ,
'id' => '1664'
),
array(
'title' => 'ggg' ,
'Info' => 'hhh' ,
'id' => '1665'
)
);
$this->db->update_batch('mytable', $data,'id');
我如何重组数组??
注意:当选择各种类型时,数组内容将保持不变。
前:
array(
'title' => 'Any value' ,
'Info' => 'Any value' ,
'id' => 'Any Id'
答案 0 :(得分:2)
这可能对您有所帮助
[akshay@localhost tmp]$ cat test.php
<?php
$a1 = array('aaa','ccc','eee','ggg');
$a2 = array('bbb','ddd','fff','hhh');
$a3 = array(1662,1663,1664,1665);
// Output
$output = array_map(function($a,$b,$c){ return array('title'=>$a,'Info'=>$b,'id'=>$c);},$a1,$a2,$a3)
print_r ( $output );
// Here add your update statement
// $this->db->update_batch('mytable', $output,'id');
?>
输出
[akshay@localhost tmp]$ php test.php
Array
(
[0] => Array
(
[title] => aaa
[Info] => bbb
[id] => 1662
)
[1] => Array
(
[title] => ccc
[Info] => ddd
[id] => 1663
)
[2] => Array
(
[title] => eee
[Info] => fff
[id] => 1664
)
[3] => Array
(
[title] => ggg
[Info] => hhh
[id] => 1665
)
)