You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Aerys中获取无键名的parseBody数组数据?

Handling Unkeyed Array POST Parameters in Aerys

Hey Jeff, I’ve dealt with this exact issue when working with Aerys—unkeyed arrays can feel tricky since the docs focus on named keys, but there’s a straightforward way to access them. Let’s break it down:

First, remember that parseBody($request) returns an Aerys\ParsedBody object. This object isn’t just a simple array, but it implements both ArrayAccess and Traversable, plus it has a handy method to get a full PHP array copy of the parsed data.

Solution 1: Get the full array copy

The easiest way to access your unkeyed array is to use the getArrayCopy() method on the parsed body object. This will give you a standard PHP array with numeric indexes (since your POST data has no key names):

$body = yield parseBody($request);
$unkeyedData = $body->getArrayCopy();

// Now you can work with it like any regular array
foreach ($unkeyedData as $item) {
    // Process each element here
}

Solution 2: Directly access via index or traverse the object

Since Aerys\ParsedBody implements ArrayAccess, you can treat it like an array directly if you know the numeric index:

$body = yield parseBody($request);
$firstElement = $body[0] ?? null; // Use null coalescing to avoid errors if index doesn't exist

You can also loop through the object directly without converting it first:

$body = yield parseBody($request);
foreach ($body as $index => $value) {
    echo "Element at index $index: $value";
}

Why get()/getArray() don’t work here

The get() and getArray() methods require a named key to fetch specific values, which you don’t have with an unkeyed array. These methods are great for structured data with named fields, but for unkeyed lists, the above approaches are much more practical.

Example Scenarios

If you’re submitting form data like this (using application/x-www-form-urlencoded):

[]=first-value&[]=second-value&[]=third-value

Or sending raw JSON like this:

["first-value", "second-value", "third-value"]

Both will be parsed into a numeric-indexed array, and the methods above will let you access all the values seamlessly.

内容的提问来源于stack exchange,提问作者Jeff

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:36:28