首页 > 解决方案 > 值未正确存储在数组 $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
      ]
    ]
  ]
]

标签: laravel

解决方案


您需要根据注册类型名称键入项目的结果:

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']);

推荐阅读