Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何对相同的值进行分组?_Php_Arrays_Laravel - Fatal编程技术网

Php 如何对相同的值进行分组?

Php 如何对相同的值进行分组?,php,arrays,laravel,Php,Arrays,Laravel,如果用户进行预订,将生成与自行车类型相同的新数据。数据库垂直增长。当进行预订时,它会生成一个唯一的密钥,并且在该时刻保持不变。例如: 作出了新的保留。”随机数0124'和类型_1:2数量,类型_2:2数量。这意味着type_1+type_2乘以数据库中存储的新行数。除自行车类型外,所有数据均相同 “bicycle_type”=>1个关系数据。这不是数量,这意味着类型1或类似的东西 基本上我想用预订号码分组。我想返回属于该预订号码的自行车类型和总数 我想合并所有数据,因为除了bicycle\u类型

如果用户进行预订,将生成与自行车类型相同的新数据。数据库垂直增长。当进行预订时,它会生成一个唯一的密钥,并且在该时刻保持不变。例如: 作出了新的保留。”随机数0124'和类型_1:2数量,类型_2:2数量。这意味着type_1+type_2乘以数据库中存储的新行数。除自行车类型外,所有数据均相同

“bicycle_type”=>1个关系数据。这不是数量,这意味着类型1或类似的东西

基本上我想用预订号码分组。我想返回属于该预订号码的自行车类型和总数

我想合并所有数据,因为除了bicycle\u类型之外,所有数据都是相同的

我有这样一个数组:

array:3 [▼
  0 => array:12 [▼
    "id" => 13
    "reservation_number" => "58f382bfc4e52"
    "bicycle_type" => 1
    "start_date" => "2017-04-23 12:00:00"
    "stop_date" => "2017-04-23 13:00:00"
    "identity_number" => "11111111110"
    "name_lastname" => "John Doe"
    "email" => "John@doe.com"
    "phone" => "(124) 124 12 41"
    "is_complete" => 1
    "created_at" => "2017-04-16 17:42:21"
    "updated_at" => "2017-04-16 17:42:21"
  ]
  1 => array:12 [▼
    "id" => 14
    "reservation_number" => "58f382bfc4e52"
    "bicycle_type" => 1
    "start_date" => "2017-04-23 12:00:00"
    "stop_date" => "2017-04-23 13:00:00"
    "identity_number" => "11111111110"
    "name_lastname" => "John Doe"
    "email" => "John@doe.com"
    "phone" => "(124) 124 12 41"
    "is_complete" => 1
    "created_at" => "2017-04-16 17:42:21"
    "updated_at" => "2017-04-16 17:42:21"
  ]
  2 => array:12 [▼
    "id" => 15
    "reservation_number" => "58f382bfc4e52"
    "bicycle_type" => 2
    "start_date" => "2017-04-23 12:00:00"
    "stop_date" => "2017-04-23 13:00:00"
    "identity_number" => "11111111110"
    "name_lastname" => "John Doe"
    "email" => "John@doe.com"
    "phone" => "(124) 124 12 41"
    "is_complete" => 1
    "created_at" => "2017-04-16 17:42:21"
    "updated_at" => "2017-04-16 17:42:21"
  ]
]
我如何像这样组织此集合:

array:1 [▼
  '58f382bfc4e52' => array:12 [▼
    "id" => 13
    "reservation_number" => "58f382bfc4e52"
    "bicycle_types" => [
        '1' => [
            'total' => 2,
        ],
        '2' => [
            'total' => 1
        ]
    ]  
    "start_date" => "2017-04-23 12:00:00"
    "stop_date" => "2017-04-23 13:00:00"
    "identity_number" => "11111111110"
    "name_lastname" => "John Doe"
    "email" => "John@doe.com"
    "phone" => "(124) 124 12 41"
    "is_complete" => 1
    "created_at" => "2017-04-16 17:42:21"
    "updated_at" => "2017-04-16 17:42:21"
  ]
]
检查循环:

<?php

$as = [
            0 => [
                "id"                 => 13,
                "reservation_number" => "58f382bfc4e52",
                "bicycle_type"       => 1,
                "start_date"         => "2017-04-23 12:00:00",
                "stop_date"          => "2017-04-23 13:00:00",
                "identity_number"    => "11111111110",
                "name_lastname"      => "John Doe",
                "email"              => "John@doe.com",
                "phone"              => "(124) 124 12 41",
                "is_complete"        => 1,
                "created_at"         => "2017-04-16 17:42:21",
                "updated_at"         => "2017-04-16 17:42:21",
            ],
            1 => [
                "id"                 => 14,
                "reservation_number" => "58f382bfc4e52",
                "bicycle_type"       => 1,
                "start_date"         => "2017-04-23 12:00:00",
                "stop_date"          => "2017-04-23 13:00:00",
                "identity_number"    => "11111111110",
                "name_lastname"      => "John Doe",
                "email"              => "John@doe.com",
                "phone"              => "(124) 124 12 41",
                "is_complete"        => 1,
                "created_at"         => "2017-04-16 17:42:21",
                "updated_at"         => "2017-04-16 17:42:21"
            ],
            2 => [
                "id"                 => 15,
                "reservation_number" => "58f382bfc4e52",
                "bicycle_type"       => 2,
                "start_date"         => "2017-04-23 12:00:00",
                "stop_date"          => "2017-04-23 13:00:00",
                "identity_number"    => "11111111110",
                "name_lastname"      => "John Doe",
                "email"              => "John@doe.com",
                "phone"              => "(124) 124 12 41",
                "is_complete"        => 1,
                "created_at"         => "2017-04-16 17:42:21",
                "updated_at"         => "2017-04-16 17:42:21",
            ],
        ];

        $result = [];

        foreach ( $as as $a ) {
        $reservationNumber = $a['reservation_number'];
        // Check if reservation number is exists in the result array or not.
        if ( ! isset($result[$reservationNumber]) ) {
            // If its not available we assign the current array to result.
            $result[$reservationNumber] = $a;
            $result[$reservationNumber]['bicycle_type'] = [];
        }

        // We check if bicycle_type is exists in the result having reservationNumber.
        // If its available we increment the value, otherwise initialize it with 1;
        if (isset($result[$reservationNumber]['bicycle_type'][$a['bicycle_type']])) {
            $result[$reservationNumber]['bicycle_type'][$a['bicycle_type']]['total'] += 1;
        } else {
            $result[$reservationNumber]['bicycle_type'][$a['bicycle_type']]['total'] = 1;
        }

    }

echo "<pre>";
        print_r($result);
echo "</pre>";
?>


我认为您的问题不够清楚。我组织了内容。请解释为什么这会有助于解决问题。虽然看起来您的代码应该可以工作,但在大多数情况下,只是闪出一些代码是不行的helpful@giorgio我理解你的担心。但是guy请求帮助生成生成给定结果的代码。没有什么可以解释的。但正如在编程中所说的,您必须对代码进行注释,以便每个人都能理解它。我在循环中添加了注释。总有一些事情需要解释,特别是当有人请求帮助时,这表明他或她不了解某些事情是如何工作的。我添加了关于我正在尝试做什么的细节。我不知道如何使这成为可能。