如何根据结果长度处理API的响应

时间:2017-03-23 11:10:25

标签: php api response

我正在使用API​​,当只找到1个结果时,它返回直接数组(也适用于嵌套属性)。当找到更多时,它返回一个带有数字索引的数组。

$response => [
    0 => [
        'title' => 'the title 1',
        'components' => [ // Array
            0 => [
                'title' => 'the title'
            ],
            1 => [
                'title' => 'the title'
            ]
            // ...
        ]
    ],
    1 => [
        'title' => 'the title 2',
        'components' => [ // Direct Array
            'title' => 'the title'
            'components' => [] // Can also be direct Array
            //...
        ]
    ],
];

是否有最佳实践"在PHP中处理这些响应。现在我必须检查每个具有嵌套属性的响应 - 这可能有多个结果,所以这可能会有很多额外的行。

0 个答案:

没有答案