JSON中的PHP计数节点返回许多

发布于 2025-02-13 23:47:50 字数 1488 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雪花飘飘的天空 2025-02-20 23:47:50

如果是 cossociative array

function isAssoc($array) {
    $array = array_keys($array);
    return ($array !== array_keys($array));
}

if (isAssoc($ONEANSWER['answers']['answer'])) {
    $ONEANSWER['answers']['answer'] = [$ONEANSWER['answers']['answer']];
}
if (isAssoc($THREEANSWERS['answers']['answer'])) {//it's not associative array
    $THREEANSWERS['answers']['answer'] = [$THREEANSWERS['answers']['answer']];
}

使用 isassoc

If it's associative array make it sequential array

function isAssoc($array) {
    $array = array_keys($array);
    return ($array !== array_keys($array));
}

if (isAssoc($ONEANSWER['answers']['answer'])) {
    $ONEANSWER['answers']['answer'] = [$ONEANSWER['answers']['answer']];
}
if (isAssoc($THREEANSWERS['answers']['answer'])) {//it's not associative array
    $THREEANSWERS['answers']['answer'] = [$THREEANSWERS['answers']['answer']];
}

using isAssoc

一笔一画续写前缘 2025-02-20 23:47:50

好的,只需参考即可根据多个注释,“答案”节点在响应json中的格式不同,如果有多个“答案”在多个答案示例中注意到方括号。

设置一个阵列的一个答案使它可以

echo count(array($ONEANSWER['answers']['answer'])); //expect 1 get 1

在生产中工作,我可以检查它是否是阵列,如果不是一个阵列。

OK just for reference so as per multiple comments the 'answer' node is formatted differently in the response JSON if there are more than one 'answers' note the square brackets on the multiple answer example.

Setting the one answer to an array got it to work

echo count(array($ONEANSWER['answers']['answer'])); //expect 1 get 1

in production I can just check if it is an array and if not make it one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文