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

如何访问JSON多维数组中destination值?解决invalid offset错误

Hey Stefano, I feel your pain—there’s nothing more frustrating than staring at a nested array from a JSON response, knowing the value you need is in there somewhere, but hitting that "invalid offset" error over and over. Let’s walk through how to fix this.

First, the most common culprit here is not fully understanding the actual structure of your multidimensional array. That error usually means you’re trying to access an index or key that doesn’t exist at the level you’re looking at.

Step 1: Visualize the full array structure

Before you try to grab the "destination" value, print out the entire parsed array in a readable format. This will show you exactly how nested the data is, which levels are arrays vs. objects, and what keys/indexes exist.

For example, if you’re using PHP, run:

print_r($yourParsedArray);
// Or for more detail:
var_dump($yourParsedArray);

If you’re working in JavaScript, use:

console.log(JSON.stringify(yourParsedArray, null, 2));

Let’s say the output looks something like this (a common structure for routing APIs):

Array
(
    [routes] => Array
        (
            [0] => Array
                (
                    [legs] => Array
                        (
                            [0] => Array
                                (
                                    [destination] => Array
                                        (
                                            [address] => "123 Main St, Anytown USA"
                                        )
                                )
                        )
                )
        )
)

In this case, trying to access $yourParsedArray['destination'] directly would throw an error—you need to traverse each nested level first.

Step 2: Traverse the array safely

Never assume every level exists. Use checks to avoid "invalid offset" errors. Here’s how to do it safely:

Option 1: Existence checks (PHP example)

// Check each nested level before accessing
if (isset($yourParsedArray['routes'], $yourParsedArray['routes'][0], $yourParsedArray['routes'][0]['legs'], $yourParsedArray['routes'][0]['legs'][0]['destination'])) {
    $destination = $yourParsedArray['routes'][0]['legs'][0]['destination'];
    echo $destination['address']; // If destination is an object/array itself
} else {
    echo "Could not find the destination value—check the array structure!";
}

Option 2: Null coalescing operator (cleaner PHP 7+)

// Use ?? to fall back to a default if any level is missing
$destination = $yourParsedArray['routes'][0]['legs'][0]['destination']['address'] ?? "Destination not found";
echo $destination;

Option 3: Loop through arrays if multiple entries exist

If there are multiple routes/legs (e.g., routes has multiple index entries), loop through them instead of hardcoding [0]:

foreach ($yourParsedArray['routes'] as $route) {
    foreach ($route['legs'] as $leg) {
        if (isset($leg['destination'])) {
            echo $leg['destination']['address'] . "\n";
        }
    }
}

Key things to watch out for:

  • Empty arrays: If routes or legs is empty, accessing index [0] will throw an error. Always check if the array is non-empty first with !empty().
  • Mix of arrays and objects: Sometimes JSON parses into objects (like stdClass in PHP) instead of associative arrays. If that’s the case, you’ll use -> instead of [] to access properties: $route->legs[0]->destination.
  • Typos: Double-check that the key is spelled exactly as "destination" (case-sensitive in most languages!).

Once you map out the structure from the printed output, you’ll be able to target the "destination" value without hitting those annoying offset errors.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 08:48:55