douchengjue9892 2014-09-08 03:02
浏览 9
已采纳

在PHP中重新排列数组[关闭]

I have an array which is multidimensional as given below:

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] => 
        )

)

I am looking a way to rearrange this array as :

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条回答 默认 最新

  • dtxa49711 2014-09-08 03:19
    关注

    You can just get the keys, then loop each key and shift each batch until it runs out. Example: 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条)

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动