douxuanou2787 2014-06-25 18:22
浏览 26
已采纳

将数组中的数组移动到php中的父数组

    echo "<pre>";
    print_r($data);
    echo "</pre>";

    Array
    (
        [0] => Array
            (
                [Deposit] => Array
                    (
                        [id] => 63393
                        [amount] => 200.00
                        [date] => 2014-06-17 08:40:13
                        [details] => 
                    )

                [User] => Array
                    (
                        [username] => presido490
                        [group_id] => 1
                    )

            )
    )

How to move [username] => presido490 from [User] => Array() to [Deposit] => Array() , i need output like that

Array
(
    [0] => Array
        (
            [Deposit] => Array
                (
                    [id] => 63393
                    [amount] => 200.00
                    [date] => 2014-06-17 08:40:13
                    [username] => presido490
                    [details] => 
                )

            [User] => Array
                (
                    [group_id] => 1
                )

        )
)
  • 写回答

1条回答 默认 最新

  • duanmianxue2687 2014-06-25 18:25
    关注

    Do it in a simple loop

    foreach($dataas $key => $element) {
        $data[$key]['Deposit']['username'] = $data[$key]['User']['username'];
        unset($data[$key]['User']['username']);
    }
    

    you could do it in an array_walk() or array_map() instead of a loop if you'd prefer

    EDIT

    If you absolutely have to position username between date and details (suggesting you don't really understand how to use array data in the first place).

    foreach($dataas $key => $element) {
        $data[$key]['Deposit'] = array(
            'id' => $data[$key]['Deposit']['id'],
            'amount' => $data[$key]['Deposit']['amount'],
            'date' => $data[$key]['Deposit']['date'],
            'username' => $data[$key]['User']['username'],
            'details' => $data[$key]['Deposit']['details'],
        );
        unset($data[$key]['User']['username']);
    }
    

    But this should make no difference to your code at all, other than adding overhead

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下