【问题标题】:values are not being stored correctly in the array $items值未正确存储在数组 $items 中
【发布时间】:2018-07-04 23:36:42
【问题描述】:

我想用下面的代码获取每种注册类型的数量:

$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


    【解决方案1】:

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

    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 是一个有效的数组,但因为它被转换为 json,11 将作为一个对象键入。要强制进行键调整,您可以执行以下操作:

    // It is hacky but it re-keys the items array numerically.
    $items['invoice']['items'] = array_values($list['invoice']['items']);
    

    【讨论】:

    • 谢谢,它有效,但你能解释一下为什么需要这样做吗?
    • api post请求出现错误“422 Unprocessable Entity` response: {"errors":[{"error":"Items element should be of type array"}]}".
    • 但是 "array:1 [▼ "invoice" => array:4 [▼ "items" => array:2 [▼ 0 => array:3 [▼ "name" => "一般" "unit_price" => 10 "quantity" => 2 ] 1 => array:3 [▼ "name" => "plus" "unit_price" => 0 "quantity" => 2 ] ] ]" 错误不要出现了,也许问题是数组不以索引 0 开头?
    • 使用新的items 输出编辑和更新您的问题,然后是您用于发布请求的验证。
    • 谢谢,我更新了这个问题。这似乎是因为它以“1”而不是“0”开头。例如,使用此代码,错误不会出现“ $i=0; foreach ($registrationTypeDetails->participants as $registrationType) { $items['invoice']['items'][$i] = [ 'name' => $registrationType->registration_type->name, 'unit_price' => $registrationType->registration_type->price, 'quantity' => $type_counts[$registrationType->registration_type->name], ]; $i++;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    • 2016-02-18
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多