doupu9251 2017-06-23 06:05
浏览 31

无法从cakephp2中的find('all')合并嵌套数组数据

I try to merge the data's in the arrays 'c' and 'a' inside MyData with the following code, but the outcome was still corrupted.

Are there something wrong with my code ? Or am I simple making a mistake with how I merge the arrays ? I'm doing all sorts of stuff to solve the problem but cannot find any solution that works. Some examples or tips will be great!

Want to merge [my_test] and [my_date] inside [MyData].

Array
(
    [0] => Array
        (
            [MyData] => Array
                (
                    [id] => 79
                    [my_birth_day] => 1990-06-20
                    [my_address] => 400
                    [my_age] => 26
                    [my_name] => Joy
                    [my_id] => 1
                    [created] => 2017-06-19 15:39:44
                )

            [c] => Array
                (
                    [my_test] => math
                )

            [a] => Array
                (
                    [my_date] => 2017-08-13
                )

        ).....Loops

    [1] => Array
        (

I would want the result to be like

Array
(
    [0] => Array
        (
    [MyData] => Array
                (
                    [id] => 79
                    [my_birth_day] => 1990-06-20
                    [my_address] => 400
                    [my_age] => 26
                    [my_name] => Joy
                    [my_id] => 1
                    [created] => 2017-06-19 15:39:44
                    [my_test] => math
                    [my_date] => 2017-08-13

I made a logic to merge the arrays and display it as the above code , but wasn't able to merge

$res = $this->find( 'all', $cond); // All the data are fetchd from this $res
            $count = count($res);
            for($i=0;$i<$count;$i++){
               $result[] =  $res[$i] ;

                $fixed_arrays[] = $result[$i]['MyData'];
                if (!empty($result[$i]['c'])) {
                    $corrupt_c_array = $result[$i]['c'];
                    $fixed_arrays = array_merge($fixed_arrays,$corrupt_c_array);
                }
                if(!empty($result[$i]['a'])) {
                    $corrupt_a_array = $result[$i]['a'];
                    $fixed_arrays = array_merge($fixed_arrays, $corrupt_a_array);
                }
            }
            $result['data'] = $fixed_arrays;  // This $result['data'] should show the expected result.

[Update] Heard about a function called set::combine for cakephp2, Is there a way to use set::combine since it's cakephp2?

  • 写回答

3条回答 默认 最新

  • douben7493 2017-06-23 06:18
    关注

    You can create the temporary $data array and merge everything and then assign to the $fixed_arrays list

    <?php
    
        $res = $this->find( 'all', $cond); // All the data are fetched from this $res
        $count = count($res);
        for($i=0;$i<$count;$i++){
           $result[] =  $res[$i] ;
           $data =array(); //temporary array
    
            $data['MyData'] = $result[$i]['MyData'];
            if (!empty($result[$i]['c']) && isset($result[$i]['c']['my_test'])) {
                $corrupt_c_array = $result[$i]['c'];
                $data['MyData']['my_test'] = $result[$i]['c']['my_test'];            
            }
            if(!empty($result[$i]['a']) && isset($result[$i]['a']['my_date'])) {
                $corrupt_a_array = $result[$i]['a'];
                $data['MyData']['my_date'] = $result[$i]['a']['my_date']; 
            }
            $fixed_arrays[] = $data; 
    
        }
        $result['data'] = $fixed_arrays; 
    
    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致