首頁 >後端開發 >php教程 >php數組遍歷後重新組裝,請教方法

php數組遍歷後重新組裝,請教方法

WBOY
WBOY原創
2016-12-05 13:44:091492瀏覽

如圖程式碼,組裝成下面這種格式,有沒有什麼辦法,數組內長度不定

<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