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; 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘