我想插入并从一个表格插入批次到2个表格,后期数据包含数组2维度, print_r 的结果发布数据
Array
(
[foo] => blalala
[bar] => xxxxxx
[date] => 2014-06-30
[time] => 08:34:30
[fruit] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
[car] => Array
(
[0] => 2
[1] => 3
[2] => 5
[3] => 7
)
[food] => Array
(
[0] => 2
[1] => 3
)
[drink] => Array
(
[0] => 3
)
[snack] => Array
(
[0] => 2
)
)
和我想要的结果
$array_master = array(
[foo] => blalala,
[bar] => xxxxxx,
[date] => 2014-06-30,
[time] => 08:34:30,
);
以及来自第二维数组[水果],[食品],[汽车],[饮料],[零食]的下一个数组计数数据
[0] => Array
(
[agenda_terkait_type] => **(if [fruit] ==1,[food] ==2,[car] ==3,[drink] ==4,[snack] ==5)**
[agenda_terkait_agenda_id] =>
[agenda_terkait_data_id] => (value of [fruit],[food],[car],[drink],[snack])
[agenda_terkait_status] => 1
)
[1] => Array
(
[agenda_terkait_type] =>
[agenda_terkait_agenda_id] => 121
[agenda_terkait_data_id] =>
[agenda_terkait_status] => 1
)
[2] => Array
(
[agenda_terkait_type] =>
[agenda_terkait_agenda_id] => 121
[agenda_terkait_data_id] =>
[agenda_terkait_status] => 1
)
[3] => Array
(
[agenda_terkait_type] =>
[agenda_terkait_agenda_id] => 121
[agenda_terkait_data_id] =>
[agenda_terkait_status] => 1
)
[4] => Array
(
[agenda_terkait_type] =>
[agenda_terkait_agenda_id] => 121
[agenda_terkait_data_id] =>
[agenda_terkait_status] => 1
)
[5] => Array
(
[agenda_terkait_type] =>
[agenda_terkait_agenda_id] => 121
[agenda_terkait_data_id] =>
[agenda_terkait_status] => 1
)
[6] => Array
(
[agenda_terkait_type] =>
[agenda_terkait_agenda_id] => 121
[agenda_terkait_data_id] =>
[agenda_terkait_status] => 1
)
[7] => Array
(
[agenda_terkait_type] =>
[agenda_terkait_agenda_id] => 121
[agenda_terkait_data_id] =>
[agenda_terkait_status] => 1
)
[8] => Array
(
[agenda_terkait_type] => 1
[agenda_terkait_agenda_id] => 121
[agenda_terkait_data_id] =>
[agenda_terkait_status] => 1
)
[9] => Array
(
[agenda_terkait_type] => 1
[agenda_terkait_agenda_id] => 121
[agenda_terkait_data_id] =>
[agenda_terkait_status] => 1
)
[10] => Array
(
[agenda_terkait_type] => 2
[agenda_terkait_agenda_id] => 121
[agenda_terkait_data_id] =>
[agenda_terkait_status] => 1
)
[11] => Array
(
[agenda_terkait_type] => 2
[agenda_terkait_agenda_id] => 121
[agenda_terkait_data_id] =>
[agenda_terkait_status] => 1
)
感谢您的帮助。我很感激。抱歉我的英语:)
答案 0 :(得分:0)
您可以使用foreach循环来实现此目的:试试这个:
$original_values = array(
'foo' => 'blalala',
'bar' => 'xxxxxx',
'date' => '2014-06-30',
'time' => '08:34:30',
'fruit' => array(1, 2, 3, 4),
'car' => array(2, 3, 5, 7),
'food' => array(2, 3),
'drink' => array(3),
'snack' => array(2),
);
$array_master = array();
foreach ($original_values as $key => $value) {
if(!is_array($value)) {
$array_master[$key] = $value;
unset($original_values[$key]);
}
}
$batch = array();
foreach($original_values as $key => $value) {
foreach ($value as $element) {
$type = '';
switch($key) {
case 'fruit':
$type = 1;
break;
case 'car':
$type = 2;
break;
case 'food':
$type = 3;
break;
case 'drink':
$type = 4;
break;
case 'snack':
$type = 5;
break;
}
$batch[] = array(
'agenda_terkait_type' => $type,
'agenda_terkait_agenda_id' => '',
'agenda_terkait_data_id' => $element,
'agenda_terkait_status' => 1,
);
}
}
echo '<pre>';
print_r($array_master);
print_r($batch);
输出如下:
Array
(
[foo] => blalala
[bar] => xxxxxx
[date] => 2014-06-30
[time] => 08:34:30
)
Array
(
[0] => Array
(
[agenda_terkait_type] => 1
[agenda_terkait_agenda_id] =>
[agenda_terkait_data_id] => 1
[agenda_terkait_status] => 1
)
[1] => Array
(
[agenda_terkait_type] => 1
[agenda_terkait_agenda_id] =>
[agenda_terkait_data_id] => 2
[agenda_terkait_status] => 1
)
[2] => Array
(
[agenda_terkait_type] => 1
[agenda_terkait_agenda_id] =>
[agenda_terkait_data_id] => 3
[agenda_terkait_status] => 1
)
[3] => Array
(
[agenda_terkait_type] => 1
[agenda_terkait_agenda_id] =>
[agenda_terkait_data_id] => 4
[agenda_terkait_status] => 1
)
.. to be continued