首页  >  文章  >  后端开发  >  php数组遍历后重新组装,请教方法

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

WBOY
WBOY原创
2016-12-05 13:44:091452浏览

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

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

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn