嗨frinds我有一个像这样的帖子数据
Array
(
[m_categoryid] => 81
[subcat_id_for_cat] => 87
[title] => test
[body] => test
[adtype] => 2
[1] => iphone
[2] => xxx
[7] => yes
[submit] => Submit ads
其中前5个字段是固定字段转到一个表table_1但是最后3个字段是动态字段,它们继续增加或减少字段并转到另一个表table_2
无法弄清楚如何解决这个问题
答案 0 :(得分:0)
尝试使用多维数组
Array
(
[m_categoryid] => 81
[subcat_id_for_cat] => 87
[title] => test
[body] => test
[adtype] => 2
[dynamic] => array(
[1] => iphone
[2] => xxx
[7] => yes
)
[submit] => Submit ads
)
然后
foreach($dynamic as $row)
答案 1 :(得分:0)
或者如果你不能修改数组和动态数据总是得到数字键
$arr = array
(
"m_categoryid" => "81",
"0" => "iphone",
"2" => "xxx",
"submit" => "Submit ads"
);
foreach ($arr as $key=>$row)
{
if(is_numeric($key))
{
echo "insert table_2\n";
} else {
echo "insert table_1\n";
}
}
答案 2 :(得分:0)
您可以创建2个数组。在第一个你把前5个元素,在秒中另一个。你可以在桌子上分配正确的数组。
$input = Array (
"m_categoryid" => 81,
"subcat_id_for_cat" => 87,
"title" => "test",
"body" => "test",
"adtype" => 2,
"iphone" => 1,
"submit" => "Submit ads"
);
$c = count($input);
$length = $c - 5;
$first = array_slice($input, 0, 5,true);
$last = array_slice($input, 5,$length, true);
$headfirst = array();
$valuefirst = array();
foreach($first as $key => $value){
$headfirst[] = $key;
$valuefirst[] = $value;
}
$this->table->set_heading($headfirst);
$this->table->add_row($valuefirst);
echo $this->table->generate();
$last = array_slice($input, 5,$length, true);
$headlast = array();
$valuelast = array();
foreach($last as $key => $value){
$headlast[] = $key;
$valuelast[] = $value;
}
$this->table->set_heading($headlast);
$this->table->add_row($valuelast);
echo $this->table->generate();
这就是全部