【问题标题】:JSON encoding multi selected rows into an array [duplicate]JSON将多选行编码为数组[重复]
【发布时间】:2019-09-01 08:39:53
【问题描述】:

我正在研究以下 sn-p。如何将所有受影响的行加载到 $items 数组中?

如您所见,我可以获取每个绑定的单元格,例如 $pid$psku,但我需要将它们加载到 $items

$items =[];
$stmt = $conn -> prepare("SELECT `pid`,`psku` FROM `appolo` ORDER BY `pid` ASC LIMIT 24");

$stmt -> execute();
$stmt -> store_result();
$stmt -> bind_result($pid, $psku);

while ($stmt -> fetch()) {
    echo $pid;
    echo $psku;
}
$stmt->free_result(); 

echo json_encode($items);

【问题讨论】:

  • 使用array_push而不是呼应它们?
  • 那么有什么问题 - [] 符号已经发明了。

标签: php mysqli


【解决方案1】:

这很简单,只需构建一个变量数组并添加到数组中:

while ($stmt -> fetch()) {
    $items[] = array($pid, $psku);
}

获取关联数组:

    $items[] = compact('pid', 'psku');

【讨论】:

  • 感谢 AbraCadaver 这正是我想要的
猜你喜欢
  • 1970-01-01
  • 2014-01-12
  • 2020-08-24
  • 1970-01-01
  • 1970-01-01
  • 2012-03-10
  • 1970-01-01
  • 2015-08-30
相关资源
最近更新 更多