Home  >  Article  >  Backend Development  >  PHP array reassembly after traversal, please tell me how to do it

PHP array reassembly after traversal, please tell me how to do it

WBOY
WBOYOriginal
2016-12-05 13:44:091411browse

As shown in the picture, the code is assembled into the following format. Is there any way? The length of the array is variable

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

Now I want to assemble it into this format:

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

Any good ideas? Thanks~

Reply content:

As shown in the picture, the code is assembled into the following format. Is there any way? The length of the array is variable

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

Now I want to assemble it into this format:

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

Any good ideas? Thanks~

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

According to the subscript, just take the value corresponding to the subscript in the array

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn