当前位置:Gxlcms > PHP教程 > php数组遍历后重新组装,请教方法

php数组遍历后重新组装,请教方法

时间:2021-07-01 10:21:17 帮助过:24人阅读

如图代码,组装成下面这种格式,有没有什么办法,数组内长度不定

$_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',
    )
);

现在想组装成这种格式的:

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
        ),
    ),
);

请问有什么好的办法?感谢~

回复内容:

如图代码,组装成下面这种格式,有没有什么办法,数组内长度不定

$_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',
    )
);

现在想组装成这种格式的:

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
        ),
    ),
);

请问有什么好的办法?感谢~

$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);

根据 下标取不同 数组对应下标的值就行了啊

$result = array();
for($i=0;$i

$result[$_POST['huodong'][$i]]=array(
    'sttime'=>$_POST['sttime'][$i], 
    'edtime'=>$_POST['edtime'][$i],                           
    'sourcelmt'=>$_POST['sourcelmt'][$i],
    );

}
print_r($result);

人气教程排行