Rumah >pembangunan bahagian belakang >tutorial php >php数组遍历后重新组装,请教方法
如图代码,组装成下面这种格式,有没有什么办法,数组内长度不定
<code>$_POST['huodong']=array( 'shenbing', 'duobao', 'ceshi' ); $_POST['sttime']=array( '2016-11-07 00:00:00', '2016-11-08 00:00:00', '2016-11-09 00:00:00' ); $_POST['edtime']=array( '2016-11-10 00:00:00', '2016-11-11 00:00:00', '2016-11-12 00:00:00' ); $_POST['sourcelmt']=array( array( 'xiaomi_uku_and', 'uc_uku_and', 'qq_uku_and' ), array( 'xiaomi_uku_and', 'qq_uku_and' ), array( 'uc_uku_and', ) );</code>
现在想组装成这种格式的:
<code>array( 'shenbing'=>array( 'sttime'=>'2016-11-07 00:00:00', 'edtime'=>'2016-11-10 00:00:00', sourcelmt=array( 'xiaomi_uku_and', 'uc_uku_and', 'qq_uku_and' ), ), 'duobao'=>array( 'sttime'=>'2016-11-08 00:00:00', 'edtime'=>'2016-11-11 00:00:00', sourcelmt=array( 'xiaomi_uku_and', 'qq_uku_and' ), ), 'ceshi'=>array( 'sttime'=>'2016-11-09 00:00:00', 'edtime'=>'2016-11-12 00:00:00', sourcelmt=array( uc_uku_and ), ), );</code>
请问有什么好的办法?感谢~
如图代码,组装成下面这种格式,有没有什么办法,数组内长度不定
<code>$_POST['huodong']=array( 'shenbing', 'duobao', 'ceshi' ); $_POST['sttime']=array( '2016-11-07 00:00:00', '2016-11-08 00:00:00', '2016-11-09 00:00:00' ); $_POST['edtime']=array( '2016-11-10 00:00:00', '2016-11-11 00:00:00', '2016-11-12 00:00:00' ); $_POST['sourcelmt']=array( array( 'xiaomi_uku_and', 'uc_uku_and', 'qq_uku_and' ), array( 'xiaomi_uku_and', 'qq_uku_and' ), array( 'uc_uku_and', ) );</code>
现在想组装成这种格式的:
<code>array( 'shenbing'=>array( 'sttime'=>'2016-11-07 00:00:00', 'edtime'=>'2016-11-10 00:00:00', sourcelmt=array( 'xiaomi_uku_and', 'uc_uku_and', 'qq_uku_and' ), ), 'duobao'=>array( 'sttime'=>'2016-11-08 00:00:00', 'edtime'=>'2016-11-11 00:00:00', sourcelmt=array( 'xiaomi_uku_and', 'qq_uku_and' ), ), 'ceshi'=>array( 'sttime'=>'2016-11-09 00:00:00', 'edtime'=>'2016-11-12 00:00:00', sourcelmt=array( uc_uku_and ), ), );</code>
请问有什么好的办法?感谢~
<code>$result = array(); foreach($_POST['huodong'] as $first_key => $id) { // 此处的 '' 和 array() 是作为默认值 foreach(array('sttime'=>'','edtime'=>'','sourcelmt'=>array()) as $second_key => $val) { $result[$id][$second_key] = isset($_POST[$second_key][$first_key]) ? $_POST[$second_key][$first_key] : $val; } } print_r($result);</code>
根据 下标取不同 数组对应下标的值就行了啊
$result = array(); }
for($i=0;$i<code>$result[$_POST['huodong'][$i]]=array(
'sttime'=>$_POST['sttime'][$i],
'edtime'=>$_POST['edtime'][$i],
'sourcelmt'=>$_POST['sourcelmt'][$i],
);</code>
print_r($result);