【问题标题】:Why are all indices in an array 0?为什么数组中的所有索引都是 0?
【发布时间】:2011-09-18 19:22:01
【问题描述】:

为什么下面的代码会输出0 索引?

我希望索引为:0 1 2 3 4 ...。我该如何解决?

代码:

foreach ($query->result() as $row){
    $data = json_decode($row->residence,true);
    foreach($data as $datum){
        $newArray = array_chunk($datum['units'], 3);
        foreach($newArray as $newA){
            $output = array(implode(",",$newA));
            echo print_r($output).'<br>'; //this is output
        }
    }
}

输出:

数组 ( [0] => salam,11,11 ) 1
数组 ( [0] => khobe,22,22 ) 1
数组 ( [0] => salam,55,55 ) 1
数组 ( [0] => khobe,66,66 ) 1

我想要这个输出:

数组 ( [0] => salam,11,11 ) 1
数组 ( [1] => khobe,22,22 ) 1
数组 ( [2] => salam,55,55 ) 1
数组 ( [3] => khobe,66,66 ) 1

更新:

我在数据库中编码的 JSON:

[{
    "units": ["salam", "11", "11", "khobe", "22", "22"],
}, {
    "units": ["salam", "55", "55", "khobe", "66", "66"],
}]

【问题讨论】:

  • 关于 0 还是 1 是数组的最佳起始索引的争论非常激烈。我的 0.5 建议在没有经过适当考虑的情况下被驳回。
  • 更改这一行:$output = explode(",",$newA));
  • 您希望索引是什么?
  • 我没有关注。您是否需要一个包含所有行或不同数组的单个数组,每个数组都从不同的索引开始?
  • 说实话,我认为您需要回归基础。听起来您不了解自己的代码中发生了什么。进一步询问您应该能够自己调试的问题只会让您和我们更加困惑。

标签: php arrays codeigniter foreach


【解决方案1】:

在这一行$output = array(implode(",",$newA)); 上,您正在为每组结果创建一个新数组,因此偏移量始终从 0 开始。

试试这个:

$output = array();

foreach ($query->result() as $row){
    $data = json_decode($row->residence,true);
    foreach($data as $unit) {
        $output[] = implode(',', $unit['units']);
    }
}

print_r($output);

我从单行得到以下输出:

Array
(
    [0] => salam,11,11,khobe,22,22
    [1] => salam,55,55,khobe,66,66
)

【讨论】:

  • 输出你的代码是你的:Array ( [0] =&gt; salam,11,11 ) Array ( [0] =&gt; salam,11,11 [1] =&gt; khobe,22,22 ) Array ( [0] =&gt; salam,11,11 [1] =&gt; khobe,22,22 [2] =&gt; salam,55,55 ) Array ( [0] =&gt; salam,11,11 [1] =&gt; khobe,22,22 [2] =&gt; salam,55,55 [3] =&gt; khobe,66,66 )
  • 你能展示一下其中一个 json 字符串的样子吗?你希望你的最终输出是什么?我认为您可能不需要 foreach($data as $datum) 或 array_chunk 部分,具体取决于数据的样子。
【解决方案2】:

在第三个foreach中,你需要将$output添加到一个新的数组中,然后在循环之后添加print_r。

例如

foreach ($query->result() as $row){
        $data = json_decode($row->residence,true);
        foreach($data as $datum){
        $newArray = array_chunk($datum['units'], 3);
        $count = 0;
        foreach($newArray as $newA){
            $output = array($count =>implode(",",$newA));
            $count++;
            print_r($output).'<br>'; //this is output
        }

        }
    }

或类似的。 您可能需要移动 $count = 0;

【讨论】:

  • 这段代码输出是这样的:Array ( [0] =&gt; salam,11,11 [1] =&gt; khobe,22,22 ) 1 Array ( [0] =&gt; salam,11,11 [1] =&gt; khobe,22,22 [2] =&gt; salam,55,55 [3] =&gt; khobe,66,66 ) 1
  • 你只想要最后一组?
  • 将 print_r 移动到 sn-p 的底部。
  • 我 /think/ 这可能是您要求的(未经测试)。虽然我无法想象你为什么要那样。 (查看更新的答案)
  • Array ( [0] =&gt; salam,11,11 ) Array ( [1] =&gt; khobe,22,22 ) Array ( [0] =&gt; salam,55,55 ) Array ( [1] =&gt; khobe,66,66 ) 请在更新帖子中查看我的 json 编码。
【解决方案3】:

试试这个

$output[] = array(implode(",",$newA));

【讨论】:

    【解决方案4】:

    您可以通过使用像这样的标准 for 循环而不是 foreach 循环来做到这一点:

    for ($i = 0; $i < count($newArray); $i++) {
      // ...
    }
    

    【讨论】:

    • 我改成:for ($i = 0; $i &lt; count($newArray); $i++) { $output = array(implode(",",$newA)); echo print_r($output).'&lt;br&gt;'; } 有错误:Message: implode() [function.implode]: Invalid arguments passed & Undefined variable: newA
    【解决方案5】:

    每次调用array 函数时,您都会创建所有单独的数组。要将项目添加到同一个数组,请使用$output[] = $valueToAdd

    $output = array();
    foreach ($query->result() as $row){
        $data = json_decode($row->residence,true);
        foreach($data as $datum){
            $newArray = array_chunk($datum['units'], 3);
            foreach($newArray as $newA){
                $output[] = implode(",",$newA);
            }
        }
    }
    print_r($output).'<br>'; //this is output
    

    【讨论】:

    • 你的代码输出:Array ( [0] =&gt; salam,11,11 ) 1 Array ( [0] =&gt; salam,11,11 [1] =&gt; khobe,22,22 ) 1 Array ( [0] =&gt; salam,11,11 [1] =&gt; khobe,22,22 [2] =&gt; salam,55,55 ) 1 Array ( [0] =&gt; salam,11,11 [1] =&gt; khobe,22,22 [2] =&gt; salam,55,55 [3] =&gt; khobe,66,66 ) 1
    • 是的,确实如此。 echo 应该在循环之外。循环没问题,arrat 的创建也没问题,但是每次将一个值推送到数组上时,整个数组都会再次回显。
    • print_r 回声。前面的回声是不必要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多