改变数组的结构

时间:2014-11-27 09:13:51

标签: php arrays mysqli structure

这是公社的国籍。

我从多表格帖子

获得此数组
Array
(
    [pid] => Array
        (
        )

    [commune] => Array
        (
            [0] => 64
            [1] => 64
            [2] => 64
            [3] => 64
e.t.c ...
        )

    [nationality] => Array
        (
            [0] => AF
            [1] => AL
            [2] => DE
            [3] => US
e.t.c ...
        )

    [female] => Array
        (
            [0] => 4
            [1] => 0
            [2] => 119
            [3] => 13
e.t.c ...
        )

    [male] => Array
        (
            [0] => 2
            [1] => 0
            [2] => 102
            [3] => 16
e.t.c ...
        )

    [send] => Array
        (
        )

)

我需要改变这样的结构:

Array
(
    [0] => Array
        ( 
            [commune] => 64
            [nationality] => AF
            [female] => 4
            [male] => 2
        )

    [1] => Array
        ( 
            [commune] => 64
            [nationality] => AL
            [female] => 0
            [male] => 0
        )

    [2] => Array
        ( 
            [commune] => 64
            [nationality] => DE
            [female] => 119
            [male] => 102
        )

    [3] => Array
        ( 
            [commune] => 64
            [nationality] => US
            [female] => 13
            [male] => 16
        )
)

e.t.c ...

如果在Array [1] [female] = 0 amd [male] = 0我需要删除数组[1]

我需要在mysqli中插入来自[commune],[nationality],[female]和[male]的值
列表名称与公社,国籍,女性,男性相同

感谢帮助
马克

1 个答案:

答案 0 :(得分:0)

以下是代码:

$ar_val = array('pid' => array(), 'commune' => array(64,64,64,64),'nationality' => array('AF','AL','DE','US'),'female' => array(4,0,119,13),'male' => array(2,0,102,16), 'send' => array());

foreach($ar_val as $ar_key=>$ar_val){
    $i = 0;
    foreach($ar_val as $arry_val){
        $output[$i][$ar_key] = $arry_val;       
        $i++;
    }   
}
print_r($output);

$ar_val 是您拥有表单值的主数组。

您也可以尝试测试此代码。它为我工作。

以上代码将在数组输出下方更改:

Array
(
    [commune] => Array
        (
            [0] => 64
            [1] => 64
            [2] => 64
            [3] => 64
        )

    [nationality] => Array
        (
            [0] => AF
            [1] => AL
            [2] => DE
            [3] => US
        )

    [female] => Array
        (
            [0] => 4
            [1] => 0
            [2] => 119
            [3] => 13
        )

    [male] => Array
        (
            [0] => 2
            [1] => 0
            [2] => 102
            [3] => 16
        )
)

到以下数组:

Array
(
    [0] => Array
        ( 
            [commune] => 64
            [nationality] => AF
            [female] => 4
            [male] => 2
        )

    [1] => Array
        ( 
            [commune] => 64
            [nationality] => AL
            [female] => 0
            [male] => 0
        )

    [2] => Array
        ( 
            [commune] => 64
            [nationality] => DE
            [female] => 119
            [male] => 102
        )

    [3] => Array
        ( 
            [commune] => 64
            [nationality] => US
            [female] => 13
            [male] => 16
        )
)