简体   繁体   English

将记录存储在父数组中

[英]Store record inside parent array

I have column date_added which has this format date 2017-02-09 08:45:48. 我有date_add列,其格式为date 2017-02-09 08:45:48。 I want to store all records of same month inside that specific month, like Feb month records should be inside Feb array. 我想将同一月份的所有记录存储在该特定月份内,例如2月月份记录应该在2月数组内。 I'm trying following code. 我正在尝试遵循以下代码。

$Res = array();
        $query   = $this->pdoConnection->prepare("select id,date from `record` ");
        $query->execute();
        header("Content-type: application/json; charset=utf-8");
        $month = array(1,2,3,4,5,6,7,8,9,10,11,12);
        while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
            $Data = $row;
            $array_date = date("m", strtotime($row['date']));
            $alpha_month = date("M", strtotime($row['date']));
            if(in_array($array_date,$month)){
                $Res[] = array($alpha_month=>array($Data));
            }
        }

        $output = array(
            'response' => $Res
        );  

        echo json_encode($output,JSON_PRETTY_PRINT);

Result 结果

{
    "response": [
        {
            "Jan": [
                {
                    "id": "1",
                    "date_added": "2017-01-03 08:43:09"
                }
            ]
        },
        {
            "Feb": [
                {
                    "id": "4",
                    "date_added": "2017-02-04 10:53:44"
                }
            ]
        },
        {
            "Feb": [
                {
                    "id": "2",
                    "date_added": "2017-02-12 08:59:15"
                }
            ]
        }
    ]
}

As you can see my code is return separate array of Feb, but i wanted to store both of inside Feb array. 如您所见,我的代码是返回2月的单独数组,但是我想将2月数组都存储在里面。 I have tried different logic but still facing same issue. 我尝试了不同的逻辑,但仍然面临相同的问题。

You can try 你可以试试

$Res[$alpha_month][] = $data;

That will just add an entry to your array for that month, you also don't need the if(in_array($array_date,$month)) anymore 这只会为该月的数组添加一个条目,您也不再需要if(in_array($array_date,$month))

A little bit more detailed to my short comment above: 我上面的简短评论更加详细:

Instead $Res[] = array($alpha_month=>array($Data)); 而是$ Res [] = array($ alpha_month => array($ Data)); you could do $Res[$alpha_month][] = $Data; 你可以做$ Res [$ alpha_month] [] = $ Data;

Currently you're fetching all result tuples in this while loop which is accurate and okay. 当前,您正在此while循环中获取所有结果元组,这是正确且可以的。 But if you want to "group" some results in an array, one way to go with is: 但是,如果要对数组中的某些结果进行“分组”,可以采用的一种方法是:

$Res[$alpha_month][] = $Data;

With this code you're "pushing" the result ($Data) in your $Res array. 使用此代码,您将结果($ Data)“推送”到$ Res数组中。 The important part is the first array layer $Res[$alpha_month] which relates to the new structure: 重要的部分是与新结构相关的第一个数组层$Res[$alpha_month]

array('JAN', 'FEB', ...);

The second [] declares for the first layer, in this case it's your month group, another array. 第二个[]声明第一层,在这种情况下,它是您的month组,另一个数组。 You may will get PHP WARNINGS by pushing your result for every month in the first time, because $Res[$alpha_month][] leads to a two-level declaration inside your array. 您可能会通过首次每月推送结果来获得PHP WARNINGS ,因为$Res[$alpha_month][]会导致数组内部有两个级别的声明。

In the first declaration, as mentioned earlier, you're pushing all groups / months into the array! 如前所述,在第一个声明中,您要将所有组/月推入数组! But in the same time, php expects the first iterative, to be an array as well: ( $Res[$alpha_month][] ) 但是在同一时间,php希望第一个迭代也是数组:( $Res[$alpha_month][]

So the clean solution would be the following: 因此,干净的解决方案如下:

if(!isset($Res[$alpha_month])){
    $Res[$alpha_month] = array(); // You can also use []; instead array();
}
$Res[$alpha_month][] = $Data;

With that snippet, you're first checking the $Res[$alpha_month] for existence - which also declares an array, if the current layer is not available. 使用该代码段,您首先要检查$Res[$alpha_month]是否存在-如果当前层不可用,它还会声明一个数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM