在PHP中重新排列数组

时间:2014-09-08 03:02:23

标签: php arrays for-loop foreach

我有一个多维的数组,如下所示:

Array
(
    [fk_member_id] => 1
    [f_name] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
            [4] => 
            [5] => 
        )

    [m_name] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
            [4] => 
            [5] => 
        )

    [l_name] => Array
        (
            [0] => Aagiwal
            [1] => Aagiwal
            [2] => Aagiwal
            [3] => Aagiwal
            [4] => Aagiwal
            [5] => Aagiwal
        )

    [dob] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
            [4] => 
            [5] => 
        )

    [gender] => Array
        (
            [0] => Male
            [1] => Male
            [2] => Male
            [3] => Male
            [4] => Male
            [5] => Male
        )

    [relation] => Array
        (
            [0] => Father
            [1] => Father
            [2] => Father
            [3] => Father
            [4] => Father
            [5] => Father
        )

    [marital_status] => Array
        (
            [0] => Single
            [1] => Single
            [2] => Single
            [3] => Single
            [4] => Single
            [5] => Single
        )

    [eduction] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
            [4] => 
            [5] => 
        )

)

我正在寻找一种方法来重新排列这个数组:

Array
(

    [0] => Array
        (
            [f_name] => 
            [m_name] => 
            [l_name] => 'Agaliwal'
            [dob] => 
            [gender] => 'Male'
            [relation] => 'Father'
            [marital_status] => 
            [education] =>        
            [fk_member_id] => 1
        )   

    [1] => Array
        (
            [f_name] => 
            [m_name] => 
            [l_name] => 'Agaliwal'
            [dob] => 
            [gender] => 'Male'
            [relation] => 'Father'
            [marital_status] => 
            [education] =>
            [fk_member_id] => 1
        )

    [2] => Array
        (
            [f_name] => 
            [m_name] => 
            [l_name] => 'Agaliwal'
            [dob] => 
            [gender] => 'Male'
            [relation] => 'Father'
            [marital_status] => 
            [education] =>
            [fk_member_id] => 1
        )

    [3] => Array
        (
            [f_name] => 
            [m_name] => 
            [l_name] => 'Agaliwal'
            [dob] => 
            [gender] => 'Male'
            [relation] => 'Father'
            [marital_status] => 
            [education] => 
            [fk_member_id] => 1
        )

    [4] => Array
        (
            [f_name] => 
            [m_name] => 
            [l_name] => 'Agaliwal'
            [dob] => 
            [gender] => 'Male'
            [relation] => 'Father'
            [marital_status] => 
            [education] =>
            [fk_member_id] => 1
        )

    [5] => Array
        (
            [f_name] => 
            [m_name] => 
            [l_name] => 'Agaliwal'
            [dob] => 
            [gender] => 'Male'
            [relation] => 'Father'
            [marital_status] => 
            [education] => 
            [fk_member_id] => 1
        )

)

3 个答案:

答案 0 :(得分:1)

请看一下以下答案:

Merge and group array data

Recursive array merge, group by key when multidimensionnal

简单搜索带来了这些链接,并为您所寻找的内容提供答案:)

答案 1 :(得分:1)

您可以获取密钥,然后循环每个密钥并移动每个批次,直到它用完为止。示例:Output

$new_values = array();
$fk_member_id = array_shift($values); // get the fk
$keys = array_keys($values); // get the keys
$count = count($values[$keys[0]]); // count columns
for($x = 0; $x < $count; $x++) {
    $new_values[$x]['fk_member_id'] = $fk_member_id; // constant
    foreach($keys as $k) {
        $new_values[$x][$k] = array_shift($values[$k]); // shift each
    }
}

echo '<pre>';
print_r($new_values);

答案 2 :(得分:1)

您可以尝试下面的内容......

$fk_member_id = $arr['fk_member_id']; unset($arr['fk_member_id']);
$newArr = array();
$count = count($arr['f_name']);
for($i = 0; $i < $count; $i++){
    $temp = array();
    foreach($arr as $k=>$v){
       $temp[$k] = $v[$i];
    }
    $newArr[] = array_merge($temp, array('fk_member_id'=>$fk_member_id));
}
print '<pre>';
print_r($newArr);
print '</pre>';