내 PHP 스크립트에 두 개의 배열이 있다고 가정 해 보겠습니다.
foreach( $orders_this_week as $this_week ){
echo $this_week->day_of_week;
echo $this_week->average_order;
}
두 번째는
foreach( $orders_last_week as $last_week ){
echo $last_week->day_of_week;
echo $last_week->average_order;
}
위의 배열 결과를 3 개의 열 테이블에 표시하고 싶습니다. 첫 번째 열에는 요일이 포함됩니다. 예 :
$last_week->day_of_week
또는 $this_week->day_of_week
반복되지 않습니다. 즉, $last_week->day_of_week
일요일 $this_week->day_of_week
이 있고 일요일이있는 경우 HTML 테이블의 첫 번째 열에 일요일이 하나 있습니다.
두 번째 열에는 $this_week->average_order
그것에 대응하는 것이 $this_week->day_of_week
있고 세 번째 열에는 $last_week->average_order
그것에 대응하는 것 $this_week->day_of_week
입니다.
일반적인 예 : let $this_week
has {[sunday, monday],[5,4]}
및 $ last_week
has { [sunday, tuesday], [3,5]}
따라서 출력 html 테이블은 다음과 같습니다.
<table border="1" width="300px">
<tr>
<th>Sunday</th>
<td>5</td>
<td>3</td>
</tr>
<tr>
<th>monday</th>
<td>4</td>
<td></td>
</tr>
<tr>
<th>tuesday</th>
<td></td>
<td>5</td>
</tr>
</table>
어떻게 할 수 있습니까?
배열을 만들고 날짜에 색인화하고 올바른 값으로 채운 다음 배열을 반복하여 html 테이블을 만들 수 있습니다.
$days = [
'Monday'=>[],
'Tuesday'=>[],
'Wednesday'=>[],
//... rest of the days here
];
foreach( $orders_this_week as $this_week ){
$days[$this_week->day_of_week]['this_week']=$this_week->average_order;
}
foreach( $orders_last_week as $last_week ){
$days[$last_week->day_of_week]['last_week']=$last_week->average_order;
}
?>
<table>
<?php foreach ($days as $day => $value): if(!empty($value)):?>
<tr>
<td><?php echo $day;?></td>
<td><?php echo isset($value['this_week'])?$value['this_week']:''?></td>
<td><?php echo isset($value['last_week'])?$value['last_week']:''?></td>
</tr>
<?php endif; endforeach;?>
</table>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다