【问题标题】:Combine Arrays - unclear what to do组合数组 - 不清楚该怎么做
【发布时间】:2014-07-01 18:19:10
【问题描述】:

变量转储

var_dump($row);

输出

array(1) { ["company"]=> string(8) "ffr3e456" ["high_1"]=> string(8) "8.32465" }
array(2) { ["company"]=> string(8) "gg8751hw" ["high_2"]=> string(7) "7.66574" }

当前代码

if ($mysqli->multi_query($query)) {
    do {
        /* store first result set */
            if ($result = $mysqli->store_result()) {
                while ($row = $result->fetch_assoc()) {
                    for ($p=1; $p<=2; $p++)
                         {
                         echo number_format($row["high_".$p],2);

以上代码显示high_1,但不显示high_2

如果需要,我如何将以上内容组合成一个数组?

和/或我如何echohigh_1high_2

谢谢。

【问题讨论】:

  • 合并还是合并?期望的输出是什么?
  • 合并会更好(不知道这是可能的)。
  • 仍然不明白你的意思(high_x 很清楚,但 company 呢?) - 请提供预期的输出和你已经尝试过的内容。
  • 以上代码可以访问high_1但不显示high_2

标签: php arrays merge


【解决方案1】:

好的,这里的问题是您有一个多重查询。因此,当您获得 $row 时,它仅包含 一个 high_x 值,这就是您无法在循环内进行迭代的原因。

要解决这个问题,您可以简单地合并行,因为 company 无关紧要并且可以被覆盖,这可能如下所示

$data = array();
while ($row = $result->fetch_assoc()) {
    $data = array_merge($data, $row);
}

现在$data 应该/可能看起来像这样(不一定按照这个顺序,但不重要)

array(
    'company' => '...',
    'high_1' => '...',
    'high_2' => '...',
    ...
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    • 2019-12-26
    • 1970-01-01
    相关资源
    最近更新 更多