指导我优化php代码,用数组中的空数据填充缺少的值

指导我优化php代码,用数组中的空数据填充缺少的值,php,arrays,Php,Arrays,我有这样的数组 array( [0] => array( [a] => r1, [b] => c1, [c] => d1, ), [1] => array( [a] => r1, [b] => c1, [c] => d2,

我有这样的数组

array(
    [0] => array(
              [a] => r1,
              [b] => c1,
              [c] => d1,
           ),
    [1] => array(
              [a] => r1,
              [b] => c1,
              [c] => d2,
           ),
    [2] => array(
              [a] => r1,
              [b] => c1,
              [c] => d3,
           ),
    [3] => array(
              [a] => r1,
              [b] => c2,
              [c] => d1,
           ),
    [4] => array(
              [a] => r1,
              [b] => c2,
              [c] => d3,
           ),
    [5] => array(
              [a] => r1,
              [b] => c3,
              [c] => d1,
           )
)
我得到的输出像

-------------------------------------
|   C1,D1   |   C1,D2   |   C1,D3   |
-------------------------------------
|     -     |   C2,D2   |     -     |
-------------------------------------
|   C3,D1   |     -     |     -     |
-------------------------------------
请帮我优化代码

我的代码:

$count = 0;
  for($i=1; $i<=3; $i++){
    for($j=1; $j<=3; $j++){
      $data[$count] = array(
        'a'     => '',
        'b'     => 'D'.$j,
        'c'     => 'C'.$i
      );
      for($r=0; $r<9; $r++){
      if(isset($rows[$r]) && $rows[$r]['b'] == 'C'.$i && $rows[$r]['c'] == 'D'.$j) {
        $data[$count] = array(
          'a'       => $rows[$r]['a'],
          'b'       => $rows[$r]['b'],
          'c'       => $rows[$r]['c']
        );
      }
    }
    $count++;
  }
}
$count=0;
对于($i=1;$i'D'.$j,
“c”=>“c”。$i
);
对于($r=0;$r$行[$r]['a'],
'b'=>$rows[$r]['b'],
'c'=>$rows[$r]['c']
);
}
}
$count++;
}
}

你想要什么?我需要优化的代码。。我是说对你来说是最小的问题?你在哪里打印数组?我正在使用空记录转储缺少的数组值。这个问题似乎与主题无关,因为它是关于代码检查的:break works。。但是我需要其他方法来减少for循环?你的意思是减少循环的数量?是的。。我需要减少循环。。因为,n个用户将访问我的代码。。因此,我需要优化代码您使用的循环总数在优化中并不重要,最重要的是它们做什么以及如何做。我将在我的帖子中重构一点您的代码,请稍等片刻……我已经更新了,它对您有帮助吗?
<?php
for($i=1; $i<=3; $i++)
{
    for($j=1; $j<=3; $j++)
    {
        $newData = null;

        // Avoid multiple calls to $rows[$i] because on each time it must browse the array,
        // use foreach instead. Pass by reference avoids copy of current row
        foreach($rows as & $iRow)
        {
            if($iRow['b'] == 'C'.$i && $iRow['c'] == 'D'.$j)
            {
                // Copy row bvecause it is the same structure
                $newData = $iRow;

                // No need to continue the $rows loop
                break;
            }
        }

        // If no results found
        if ($newData == null)
        {
            $newData = array(
                'a'     => '',
                'b'     => 'D'.$j,
                'c'     => 'C'.$i
                );
        }

        // Auto increment index
        $data[] = $newData;
    }
}