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

问题描述 投票:0回答:3

我尝试使用以下代码将数据合并到qazxsw poi内的数组'c'和'a'中,但结果仍然存在损坏。

我的代码有问题吗?或者我简单地弄错了我如何合并数组?我正在做各种各样的事情来解决问题,但找不到任何有效的解决方案。一些例子或提示将是伟大的!

想要在MyData中合并[my_test][my_date]

[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
        (

我做了一个逻辑来合并数组并将其显示为上面的代码,但无法合并

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

[更新]听说一个名为set :: combine for cakephp2的函数,有没有办法使用set :: combine,因为它是cakephp2?

php arrays cakephp-2.0
3个回答
1
投票

您可以创建临时$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. 数组并合并所有内容,然后分配给$data列表

$fixed_arrays

0
投票

如果您使用条件,那么如果它将为空,则无法获取所有值,因此请使用如下:

<?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; 

只需打印$ data或$ result即可获得所需的数组。


0
投票
   <?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[$i]['MyData'] = $result[$i]['MyData'];

    $data[$i]['MyData']['my_test'] = $result[$i]['c']['my_test'];


    $data[$i]['MyData']['my_date'] = $result[$i]['a']['my_date'];
}

$result['data'] = $data;
© www.soinside.com 2019 - 2024. All rights reserved.