php - 来自 ABAP Web 服务的响应返回多个项目的数组,但返回单个项目的对象
问题描述
我正在运行一个网络服务来连接到 ABAP 并在 PHP 上返回它。对于多个项目,它包含在对象数组中。但是,如果它只有 1,它只是一个对象。
多个项目
+"TStockResponse": {#398
+"item": array:3 [
0 => {#399
// item data here
}
1 => {#400
// item data here
}
2 => {#401
// item data here
}
]
}
与单个项目
+"TStockResponse": {#393
+"item": {#387
// item data here
}
}
想知道 PHP 上是否有一个选项来确定该项目是否只有 1,它将它包含在一个数组中并继续执行必要的 foreach() 函数。
foreach($results->TStockResponse as $resp)
{
if(count($resp) == 1)
{
// do single task
}
else
{
foreach($resp as $res)
{
// do each task
}
}
}
这是我当前的 PHP 代码,如果我尝试将单个项目发送到 ABAP,我会在 count() 上收到 ErrorException
解决方案
应该很简单
if ( !is_array( $results->TStockResponse->item ) ) {
$results->TStockResponse->item = [ $results->TStockResponse->item ];
}
推荐阅读
- pip - Python2.7-pip 未安装模块
- python - 我想在一个 json 中嵌套三个表并将该 json 传递给 HTML,以便我们可以显示表?
- xcode - Xcode:如何在文件检查器中复制文件?
- c - 使用损坏的顶部大小获取内存内存分配错误
- reactjs - 类中缺少构造函数主体
- vba - 使用 VBA 或 PowerShell 将所有 MS Access SQL 查询导出到文本文件
- ui-automation - 如何在封闭的 shadowDOM 下自动化网页
- python-3.x - pymongo:如何按名称作为字符串访问 MongoDB 集合?
- postgresql - 最后的 SQLSTATE - PostgreSQL
- javascript - 确保值在 360 度环绕时正确插值