본문 바로가기
PHP 박살내기/PHP 연습하기

php 배열 연습문제 1

by 인생여희 2017. 9. 7.
반응형

배열 연습문제


#배열문제1





#결과화면





#소스코드

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>

table{border:1px solid black}

td {border:1px solid black}

</style>

</head>
<body>


<?php

print '<br><h1>지역 인원수 출력</h1>';
$city= array('경기수원'=>1194313,
'경남창원'=>1059241,
'경기고양'=>990073,
'경기용인'=>971327,
'충북청주'=>833276,
'전북전주'=>658172,
'충남천안'=>629062,
'경남김해'=>534124,
'경북포항'=>511124,
'경남진주'=>349788
);



print '<table>';
print '<tr><td>지역</td><td>인구수</td></tr>';



$city['total']=0;

foreach($city as $key =>$value){
$city['total'] += $value;
print "<tr><td>$key</td><td>$value</td></tr>";
}

//print count($city);
print "<tr><td>";
print array_search($city['total'], $city);
print "</td><td>$city[total]</td></tr>";
print '</table>';
?>




<?php
print '<br><h1>오름차순</h1>';

$city= array('경기수원'=>1194313,
'경남창원'=>1059241,
'경기고양'=>990073,
'경기용인'=>971327,
'충북청주'=>833276,
'전북전주'=>658172,
'충남천안'=>629062,
'경남김해'=>534124,
'경북포항'=>511124,
'경남진주'=>349788
);



print '<table>';
print '<tr><td>지역</td><td>인구수</td></tr>';



$city['total']=0;

asort($city);
foreach($city as $key =>$value){
$city['total'] += $value;
print "<tr><td>$key</td><td>$value</td></tr>";
}

//print count($city);
print "<tr><td>";
print array_search($city['total'], $city);
print "</td><td>$city[total]</td></tr>";
print '</table>';
?>





<?php
print '<br><h1>도별인구수</h1>';




$city= array('경기' => array('수원' => 1194313,'고양' => 990073,'용인' => 971327),
'충북' => array('청주' => 833276),
'경남' => array('창원' => 1059241,'진주' => 349788,'김해' => 534124),
'충남' => array('천안' => 629062),
'전북' => array('전주' => 658172),
'경북' => array('포항' => 511124)
);


print '<table>';
print '<tr><td>시도</td><td>지역</td><td>인구수</td><td>시별인구수</td></tr>';


foreach($city as $sido =>$arry_city){
$city [$sido]['total']=0;
foreach($arry_city as $city_name => $city_count){
$city [$sido]['total'] += $city_count;

print "<tr><td>$sido</td><td>$city_name</td><td>$city_count</td><td></td></tr>";
}

print "<tr><td>$sido</td><td>총인원수</td><td></td><td>";
print $city [$sido]['total'];
print "</td></tr>";
}


print '</table>';
?>


</body>
</html>



문제출처: 한빛 미디어 learning php 

답안작성:직접

반응형

'PHP 박살내기 > PHP 연습하기' 카테고리의 다른 글

php 배열 연습문제 2  (0) 2017.09.07
php로 가게 메뉴 계산기 만들기 1  (0) 2017.09.04

댓글