LoginSignup
0

More than 5 years have passed since last update.

프로그래밍공부시작- array

Last updated at Posted at 2018-07-30

Here is my playground.
some code is wrong.
My English is wrong too.
I practice English and programing.
I don't want to find dictionary and search internet.
Anyone help me.
It is difficult to think as English and speak English because we are not American.

Some people told to me that It is good to write diary everyday.

foreach,for

qiita.rb
$meals = array('walunt bun' => 1,
              'sushi' => 2,
              'chips' => 3,
              'eggplant' => 4);

foreach ($meals as $dish => $price) {

  // %s 문자열로 표현 ,%2f 2자리 실수로 표현 \n -> 이거 동작 안함
  printf("The new price of %s is \$%.2f.\n",$dish,$price);
}

배열의 출력순서

qiita.rb
$letters[0] = 'A';
$letters[1] = 'B';
$letters[3] = 'D';
$letters[2] = 'C';

// 배열안에 있는 순서대로 출력
foreach ($letters as $letter) {
  print $letter;
}

// for문은 순서대로 출력
for ($i=0, $num_letters = count($letters); $i < $num_letters; $i++) { 

  print $letters[$i];

}

array_key_exists(), in_array(), array_search()

qiita.rb
$foods = array(
  'Walnut Bun' => 1,
  'Cashew Nuts and White Mushrooms' => 4.95,
  'Dried Mulberries' => 3.00,
  'Eggplant with Chili Sauce' => 6.50,
  'Shrimp Puffs' => 0); // Shrimp Puffs are free!

$books = array(
   "The Eater's Guide to Chinese Characters",
   'How to Cook and Eat in Chinese');

// 배열에 해당 키값이 있을떼
if (array_key_exists('Shrimp Puffs', $foods)) {
  print "yes we have Shrimp puffs";
}

// 배열안의 키값의순서로도 값을 찿을수 있다.
if (array_key_exists(1, $books)) {
  print $books[1];
}
// array_search() 은 true element key를 반환한다
$dish =array_search(6.50, $meals);
if ($dish) {
  print "$dish costs \$6.50";
}

배열의 수정

qiita.rb
$dishes['Beef Chow Foon'] = 12;
$dishes['Beef Chow Foon']++; // 이건 하나식 증가
$dishes['Roast Duck'] = 3;

$dishes['total' ] = $dishes['Beef Chow Foon'] + $dishes['Roast Duck'];
if ($dishes['total'] > 15) {
  print "you are a lot:";
}

print 'you ate' .$dishes['Beef Chow Foon'].'dishes of Beef Chow Foon';

$meals['breakfast'] = 'Walnut Bun';
$meals['lunch'] = 'Eggplant with Chili Sauce';
$amounts = array(3, 6);

//좀 이해가 되지 않는데 왜 배열에서 따옴표를 안넣었는데 되는거지?
print "For breakfast, I'd like $meals[breakfast] and for lunch";
print "I'd like $meals[lunch]. I want $amounts[0] at breakfast and";
print "$amounts[1] at lunch";

implode(),explode()

qiita.rb
//모든 값을 출력할때는 implode() function 을 사용한다
$dimsum = array('Chicken Bun','Stuffed Duck Web','Turnip Cake');
$menu = implode(',', $dimsum);
print $menu;

//implode() 첫번째 argrument "," 없을경우
$letters = array('A','B','C','D');
print implode('',$letters);

$dimsum = array('Chicken Bun','Stuffed Duck Web','Turnip Cake');
print '<table>';
print '<tr><td>' .implode('</td><td>',$dimsum).'</td></tr>';
print '</table>';

// emplode 의 카운터 파트너 -->> 분리해서 배열로 만들었군
$fish = 'Bass, Carp, Pike, Flounder';
$fish_list = explode(',', $fish);
print "the second fish is $fish_list[1]";

sort(),asort()

qiita.rb
$dinner = array(
  'Sweet Corn and Asparagus',
  'Lemon Chicken',
  'Braised Bamboo Fungus');

$meal = array(
  'breakfast' => 'Walnut Bun',
  'lunch' => 'Cashew Nuts and White Mushrooms',
  'snack' => 'Dried Mulberries',
  'dinner' => 'Eggplant with Chili Sauce');

foreach ($dinner as $key => $value) {
  print "\$dinner : $key $value<br>\n";
}

foreach ($meal as $key =>$value) {
 print "\$meal : $key $value<br>\n";
}

sort($dinner);
sort($meal);

print "After Sorting:<br>\n";
foreach ($dinner as $key => $value) {
print " \$dinner: $key $value<br>\n";
}
foreach ($meal as $key => $value) {
print " \$meal: $key $value<br>\n";
}

$meal = array(
  'breakfast' => 'Walnut Bun',
  'lunch' => 'Cashew Nuts and White Mushrooms',
  'snack' => 'Dried Mulberries',
  'dinner' => 'Eggplant with Chili Sauce');

foreach ($meal as $key => $value) {
  print " \$meal : $key : $value<br>\n";
}

// value 는 알파벳 순으로 정렬하고, 각 원소와 연관된 인덱스가 유지된다.
asort($meal);

print "After Sorting:<br>\n";
foreach ($meal as $key => $value) {
  print " \$meal : $key : $value<br>\n";
}

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0