Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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 - Fatal编程技术网

php基于数组中的两个键创建计数

php基于数组中的两个键创建计数,php,arrays,Php,Arrays,我有一个名为$dinnerDetails的数组,如下所示 array:5 [▼ 0 => array:7 [▼ "request_id" => "48" "dinner_id" => "36" "dinner_name" => "Black Tie" "dinner_date" => "2016-05-16T10:00:00" "host_id" => "77" "new_dinner" => "yes

我有一个名为
$dinnerDetails
的数组,如下所示

array:5 [▼
  0 => array:7 [▼
    "request_id" => "48"
    "dinner_id" => "36"
    "dinner_name" => "Black Tie"
    "dinner_date" => "2016-05-16T10:00:00"
    "host_id" => "77"
    "new_dinner" => "yes"
    "requestCount" => 3
  ]
  1 => array:7 [▼
    "request_id" => "51"
    "dinner_id" => "36"
    "dinner_name" => "Black Tie"
    "dinner_date" => "2016-05-16T10:00:00"
    "host_id" => "77"
    "new_dinner" => "no"
    "requestCount" => 3
  ]
  2 => array:7 [▼
    "request_id" => "65"
    "dinner_id" => "36"
    "dinner_name" => "Black Tie"
    "dinner_date" => "2016-05-16T10:00:00"
    "host_id" => null
    "new_dinner" => "no"
    "requestCount" => 3
  ]
  3 => array:7 [▼
    "request_id" => "50"
    "dinner_id" => "35"
    "dinner_name" => "Black Tie"
    "dinner_date" => "2016-05-27T10:00:00"
    "host_id" => "77"
    "new_dinner" => "yes"
    "requestCount" => 2
  ]
  4 => & array:7 [▼
    "request_id" => "52"
    "dinner_id" => "35"
    "dinner_name" => "Black Tie"
    "dinner_date" => "2016-05-27T10:00:00"
    "host_id" => "77"
    "new_dinner" => "no"
    "requestCount" => 2
  ]
]
array:5 [▼
  0 => array:8 [▼
    "request_id" => "48"
    "dinner_id" => "36"
    "dinner_name" => "Black Tie"
    "dinner_date" => "2016-05-16T10:00:00"
    "host_id" => "77"
    "new_dinner" => "yes"
    "requestCount" => 3
    "confirmationCount" => 2
  ]
  1 => array:8 [▼
    "request_id" => "51"
    "dinner_id" => "36"
    "dinner_name" => "Black Tie"
    "dinner_date" => "2016-05-16T10:00:00"
    "host_id" => "77"
    "new_dinner" => "no"
    "requestCount" => 3
    "confirmationCount" => 2
  ]
  2 => array:8 [▼
    "request_id" => "65"
    "dinner_id" => "36"
    "dinner_name" => "Black Tie"
    "dinner_date" => "2016-05-16T10:00:00"
    "host_id" => null
    "new_dinner" => "no"
    "requestCount" => 3
    "confirmationCount" => 2
  ]
  3 => array:8 [▼
    "request_id" => "50"
    "dinner_id" => "35"
    "dinner_name" => "Black Tie"
    "dinner_date" => "2016-05-27T10:00:00"
    "host_id" => "77"
    "new_dinner" => "yes"
    "requestCount" => 2
    "confirmationCount" => 2
  ]
  4 => & array:8 [▼
    "request_id" => "52"
    "dinner_id" => "35"
    "dinner_name" => "Black Tie"
    "dinner_date" => "2016-05-27T10:00:00"
    "host_id" => "77"
    "new_dinner" => "no"
    "requestCount" => 2
    "confirmationCount" => 2
  ]
]
您会注意到,每个子阵列都有
晚餐id
主机id
,如果这两个值都存在,则表示晚餐已确认。我想做的是数一数确定的晚餐数量。在上面的一个数组中,
晚餐id=>36
已经由两个人确认,同样的情况也发生在
晚餐id
=>35上

我想在子数组中添加一个新的
,让我们调用它
confirmationCount
,并将已确认的计数与之相对应,以便我的输出如下所示

array:5 [▼
  0 => array:7 [▼
    "request_id" => "48"
    "dinner_id" => "36"
    "dinner_name" => "Black Tie"
    "dinner_date" => "2016-05-16T10:00:00"
    "host_id" => "77"
    "new_dinner" => "yes"
    "requestCount" => 3
  ]
  1 => array:7 [▼
    "request_id" => "51"
    "dinner_id" => "36"
    "dinner_name" => "Black Tie"
    "dinner_date" => "2016-05-16T10:00:00"
    "host_id" => "77"
    "new_dinner" => "no"
    "requestCount" => 3
  ]
  2 => array:7 [▼
    "request_id" => "65"
    "dinner_id" => "36"
    "dinner_name" => "Black Tie"
    "dinner_date" => "2016-05-16T10:00:00"
    "host_id" => null
    "new_dinner" => "no"
    "requestCount" => 3
  ]
  3 => array:7 [▼
    "request_id" => "50"
    "dinner_id" => "35"
    "dinner_name" => "Black Tie"
    "dinner_date" => "2016-05-27T10:00:00"
    "host_id" => "77"
    "new_dinner" => "yes"
    "requestCount" => 2
  ]
  4 => & array:7 [▼
    "request_id" => "52"
    "dinner_id" => "35"
    "dinner_name" => "Black Tie"
    "dinner_date" => "2016-05-27T10:00:00"
    "host_id" => "77"
    "new_dinner" => "no"
    "requestCount" => 2
  ]
]
array:5 [▼
  0 => array:8 [▼
    "request_id" => "48"
    "dinner_id" => "36"
    "dinner_name" => "Black Tie"
    "dinner_date" => "2016-05-16T10:00:00"
    "host_id" => "77"
    "new_dinner" => "yes"
    "requestCount" => 3
    "confirmationCount" => 2
  ]
  1 => array:8 [▼
    "request_id" => "51"
    "dinner_id" => "36"
    "dinner_name" => "Black Tie"
    "dinner_date" => "2016-05-16T10:00:00"
    "host_id" => "77"
    "new_dinner" => "no"
    "requestCount" => 3
    "confirmationCount" => 2
  ]
  2 => array:8 [▼
    "request_id" => "65"
    "dinner_id" => "36"
    "dinner_name" => "Black Tie"
    "dinner_date" => "2016-05-16T10:00:00"
    "host_id" => null
    "new_dinner" => "no"
    "requestCount" => 3
    "confirmationCount" => 2
  ]
  3 => array:8 [▼
    "request_id" => "50"
    "dinner_id" => "35"
    "dinner_name" => "Black Tie"
    "dinner_date" => "2016-05-27T10:00:00"
    "host_id" => "77"
    "new_dinner" => "yes"
    "requestCount" => 2
    "confirmationCount" => 2
  ]
  4 => & array:8 [▼
    "request_id" => "52"
    "dinner_id" => "35"
    "dinner_name" => "Black Tie"
    "dinner_date" => "2016-05-27T10:00:00"
    "host_id" => "77"
    "new_dinner" => "no"
    "requestCount" => 2
    "confirmationCount" => 2
  ]
]
为此,我编写了两次
foreach
循环,第一个循环准备数组,第二个循环添加我需要的计数

$dinnerDetails = array();
$lastDinnerDate = '';
$newDate = '';
$countDinner = array();
$countConfirmation = array();
//Prepare an array of requests so it can be passed back to the controller
foreach ($invitations as $invitation) {

        $countDinner[$invitation['dinner_id']] = isset($countDinner[$invitation['dinner_id']]) ? $countDinner[$invitation['dinner_id']] + 1 : 1;
        $countConfirmation[$invitation['host_id']] = isset($countConfirmation[$invitation['host_id']]) ? $countConfirmation[$invitation['host_id']] + 1 : 1;

        //If the date changes then assign yes, this is used on front end to display date on top of requests
        if (strtotime($invitation['dinner_date']) > $lastDinnerDate) {
            $newDate = 'yes';
        } else {
            $newDate = 'no';
        }

        $lastDinnerDate = strtotime($invitation['dinner_date']);

        $dinnerDetails[] = array(
            'request_id' => $invitation['request_id'],
            'dinner_id' => $invitation['dinner_id'],
            'dinner_name' => $invitation['dinner_name'],
            'dinner_date' => $invitation['dinner_date'],
            'host_id' => $invitation['host_id'],
            'new_dinner' => $newDate,
        );

}

foreach ($dinnerDetails as &$dinnerDetail) {
    $dinnerDetail['requestCount'] = $countDinner[$dinnerDetail['dinner_id']];
    $dinnerDetail['confirmationCount'] = $countConfirmation[$dinnerDetail['host_id']];
}
我遇到的问题在下面的代码中

$countConfirmation[$invitation['host_id']] = isset($countConfirmation[$invitation['host_id']]) ? $countConfirmation[$invitation['host_id']] + 1 : 1;

我需要以这样一种方式更新此代码,即它还需要考虑
晚餐\u id
,以便我得到的结果“确认计数”与晚餐相关联。现在,无论确认是否与正确的晚餐链接,它都会查找主机id并返回结果。

此方法基于多维数组。因此,您正在以
$countConfirmation[77][36]
的方式向
$countConfirmation[77][36]写入信息,其中
77
host\u id
并且
36
晚餐\u id
在第二个循环中,您确切地知道
host=77
订购了多少晚餐,所以只需将这些信息添加到数组中

一种快速的方法是改变:

$countConfirmation[$invitation['host_id']] = isset($countConfirmation[$invitation['host_id']]) ? $countConfirmation[$invitation['host_id']] + 1 : 1;


更改后,您将收到双量程阵列类型:

[ "77" : 
    [ 
        "36"  : "2",
        "35"  : "2"
    ],

  "othe_host_id" : 
    [
        "other_dinner_id"  : "count_of_orders",
        "other_dinner_id_2"  : "count_of_orders_2",
    ],  
]

在第二个循环中,而不是:

$dinnerDetail['confirmationCount'] = $countConfirmation[$dinnerDetail['host_id']];
进行此操作,以便通过两个参数检查信息,
host\u id
dinner\u id

$dinnerDetail['confirmationCount'] = $countConfirmation[$dinnerDetail['host_id']][$dinnerDetail['dinner_id']];

我不太明白,如果
晚餐id
主人id
都设置好了,你只想
$countConfirmation[$invitation['host\u id']]
启动?@Epodax是的,你是对的,它已经在这样做了,但它并没有考虑到
晚餐id
,例如,现在我为
晚餐id=>36得到的
计数确认
4
,而应该是
2
。它给出4的原因是因为它对其他晚餐计算相同的
host\u id
too@FrayneKonok我不认为这是我需要的你想通过创建
$dinnerDetail
?@FrayneKonok创建后,数据显示在前端你能解释一下更改吗,它似乎给出了相同的结果。第一个更改是抛出一个通知,通知:未定义的索引:dinner\u id
@Saadia用于哪个变量?它是用于
[$invitation['dinner\u id']]
中使用的
$countConfirmation[$invitation['host\u id'][$invitation['dinner\u id']]
这意味着您有一些数组没有
晚餐id