简体   繁体   English

PHP foreach,如何从数组中的键循环

[英]PHP foreach, how to loop from key in array

I have an array returned from a DB and I'm trying to echo the output of each field, where the IDS are the same from within, and then moving on to the next ID, until finished.我有一个从数据库返回的数组,我试图回显每个字段的输出,其中 IDS 从内部是相同的,然后移动到下一个 ID,直到完成。 So it prints out all the ID "1s", then "2s", then when its finished it stops.所以它打印出所有的ID“1s”,然后是“2s”,然后当它完成时它停止。 The number of ID's is variable from 1-950. ID 的数量在 1-950 之间可变。

array ( 0 => stdClass::__set_state(array
( 'Year' => '2017', 
'Month' => 'January',
 'name' => 'Faults', 
'id' => '1', 
'value' => '14058', )),



1 => stdClass::__set_state(array( 
'Year' => '2017',
 'Month' => 'February', 
'name' => 'Faults', 
'id' => '1', 
 'value' => '21', )),




2 => stdClass::__set_state(array
( 'Year' => '2017',
 'Month' => 'January',
 name' => 'Errors', 
'id' => '2', 'value' => '22874', )), 

3 => stdClass::__set_state(array( 
'Year' => '2017', 
'Month' => 'February', 
'name' => 'Errors',
'id' => '2',  
'value' => '2650', )), )

I am trying to achieve an output like this:我正在尝试实现这样的输出:

January 2017 Faults = 14958
February 2017 Faults = 21

HTML formatting here to break up/make a dividing row in between此处的 HTML 格式用于拆分/在它们之间创建分隔行

January 2017 Errors = 22784
February 2017 Errors = 2650 

Using this foreach syntax:使用这个 foreach 语法:

foreach ($results as $result) :
    if ($result->id == 1){
        echo   '<td>', $result->Month, '</td>';

This works but I need to update the if statement, it is not a good solution这有效,但我需要更新 if 语句,这不是一个好的解决方案

How can I change this so it looks inside the array and only outputs the HTML for each "id"?如何更改它以便它在数组内部查找并且只输出每个“id”的 HTML?

I've tried foreach ($results =>id as $result) but it doesnt work.我试过foreach ($results =>id as $result)但它不起作用。 Is this the correct idea?这是正确的想法吗? What is the correct syntax?什么是正确的语法?

THANKS!谢谢!

I'm using this function from the array_multisort documentation.我正在使用array_multisort文档中的这个函数。 This should work for you.这应该对你有用。 I've sorted the array to filter by the id.我已经对数组进行了排序以按 id 过滤。 We save the id and if the next one contains it, we will output the html code.我们保存 id,如果下一个包含它,我们将输出 html 代码。

    $results = array_msort($results , array('id'=>SORT_ASC));

    function array_msort($array, $cols){
      $colarr = array();
     foreach ($cols as $col => $order) {
         $colarr[$col] = array();
         foreach ($array as $k => $row) { $colarr[$col]['_'.$k] = strtolower($row[$col]); }
      }
      $eval = 'array_multisort(';
      foreach ($cols as $col => $order) {
          $eval .= '$colarr[\''.$col.'\'],'.$order.',';
       }
       $eval = substr($eval,0,-1).');';
       eval($eval);
       $ret = array();
       foreach ($colarr as $col => $arr) {
           foreach ($arr as $k => $v) {
               $k = substr($k,1);
              if (!isset($ret[$k])) $ret[$k] = $array[$k];
                $ret[$k][$col] = $array[$k][$col];
            }
       }
       return $ret;

    }

    $oldId = 0;
    foreach ($results as $res){
        if($oldId == $res->id){
             echo   '<td>', $res->Month, '</td>';
             //plus other output display
        }
        $oldId = $res->id;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM