laravel - 值未正确存储在数组 $items 中
问题描述
我想使用以下代码获取每种注册类型的数量:
$registrationTypeDetails = Registration::with('participants:id,registration_type_id,registration_id')->find($regID);
$type_counts = [];
foreach ($registrationTypeDetails->participants as $p) {
$name = $p->registration_type->name;
if (!isset($type_counts[$name])) {
$type_counts[$name] = 0;
}
$type_counts[$name]++;
}
dd($type_counts),如果会议有 2 种可用的注册类型(普通和加号),并且用户正在注册类型为“普通”的 2 名参与者和注册类型“加号”中的 0 名参与者,则显示:
array:2 [▼
"general" => 2
]
然后我需要向 API 发出一个 post 请求,需要在请求正文中发送每个注册类型的数量,在这种情况下,只有 1 个注册类型“一般”,数量的值应该是“2”。
所以我在上面的代码下面有这个代码来创建数组:
foreach ($registrationTypeDetails->participants as $registrationType) {
$items['invoice']['items'][] = [
'name' => $registrationType->registration_type->name,
'unit_price' => $registrationType->registration_type->price,
'quantity' => $type_counts[$registrationType->registration_type->name],
];
}
$create = $client->request('POST', 'https://...', [
'query' => ['api_key' => '...'], 'json' => $items,
]);
但是输出数组是:
array:1 [▼
"invoice" => array:4 [▼
"client" => array:7 [▶]
"items" => array:2 [▼
0 => array:5 [▼
"name" => "general"
"unit_price" => 10
"quantity" => 2
]
1 => array:5 [▼
"name" => "general"
"unit_price" => 10
"quantity" => 2
]
]
]
]
而不是只有一项:
array:1 [▼
"invoice" => array:4 [▼
"client" => array:7 [▶]
"items" => array:2 [▼
0 => array:5 [▼
"name" => "general"
"unit_price" => 10
"quantity" => 2
]
]
]
]
$items 显示:
array:1 [▼
"invoice" => array:4 [▼
"items" => array:2 [▼
1 => array:5 [▼
"name" => "general"
"unit_price" => 10
"quantity" => 2
]
2 => array:5 [▼
"name" => "plus"
"unit_price" => 0
"quantity" => 2
]
]
]
]
解决方案
您需要根据注册类型名称键入项目的结果:
foreach ($registrationTypeDetails->participants as $registrationType) {
$items['invoice']['items'][$registrationType->registration_type->name] = [
'name' => $registrationType->registration_type->name,
'unit_price' => $registrationType->registration_type->price,
'quantity' => $type_counts[$registrationType->registration_type->name],
];
}
如果有,最好使用registration_type的id:
foreach ($registrationTypeDetails->participants as $registrationType) {
$items['invoice']['items'][$registrationType->registration_type->id] = [
'name' => $registrationType->registration_type->name,
'unit_price' => $registrationType->registration_type->price,
'quantity' => $type_counts[$registrationType->registration_type->name],
];
}
解释
当您遍历链接到注册类型的参与者时,多个参与者可能具有相同的注册类型。因此,在您的情况下,您需要根据注册类型对结果进行分组。只需添加到items[]
items 数组即可。您想按注册类型对结果进行分组。
修复 422 验证错误
您的 items 是一个有效的数组,但因为它被转换为 json1
并且1
将作为对象键入。要强制进行键调整,您可以执行以下操作:
// It is hacky but it re-keys the items array numerically.
$items['invoice']['items'] = array_values($list['invoice']['items']);
推荐阅读
- c++ - 我使用了 sprintf(),为什么 VC++ 抱怨 _swprintf()?
- javascript - 重新定位梅西问框
- c++ - 如何在 C++ 中将 do while 循环转换为 while 循环?
- android - 我的 Admob 广告未在我的应用中显示,出现错误代码 3“无广告配置”如何解决,我的测试广告也正常工作
- javascript - 在 vue 可拖动的每次更改后获取数组 ID
- html - 为什么“价格和时间”表头没有完全覆盖该部门
- java - 序列化和反序列化属性不一致
- flutter - Flutter:当路由被压入堆栈时如何自动触发函数?
- python - 如何从 es.search 结果中的列表中获取搜索到的字符串?
- python - 从 s3 Bucket 获取数据