• 吴裕雄--天生自然PHP-MySQL-JavaScript学习笔记:PHP数组


    <?php
      $paper['copier'] = "Copier & Multipurpose";
      $paper['inkjet'] = "Inkjet Printer";
      $paper['laser']  = "Laser Printer";
      $paper['photo']  = "Photographic Paper";
    
      echo $paper['laser'];
    ?>
    <?php
      $p1 = array("Copier", "Inkjet", "Laser", "Photo");
    
      echo "p1 element: " . $p1[2] . "<br>";
    
      $p2 = array('copier' => "Copier & Multipurpose",
                  'inkjet' => "Inkjet Printer",
                  'laser'  => "Laser Printer",
                  'photo'  => "Photographic Paper");
    
      echo "p2 element: " . $p2['inkjet'] . "<br>";
    ?>
    <?php
      $paper = array("Copier", "Inkjet", "Laser", "Photo");
      $j = 0;
    
      foreach($paper as $item)
      {
        echo "$j: $item<br>";
        ++$j;
      }
    ?>
    <?php
      $paper = array('copier' => "Copier & Multipurpose",
                     'inkjet' => "Inkjet Printer",
                     'laser'  => "Laser Printer",
                     'photo'  => "Photographic Paper");
    
      foreach($paper as $item => $description)
        echo "$item: $description<br>";
    ?>
    <?php
      $paper = array('copier' => "Copier & Multipurpose",
                     'inkjet' => "Inkjet Printer",
                     'laser'  => "Laser Printer",
                     'photo'  => "Photographic Paper");
    
      while (list($item, $description) = each($paper))
        echo "$item: $description<br>";
    ?>
    <?php
      list($a, $b) = array('Alice', 'Bob');
      echo "a=$a b=$b";
    ?>
    <?php
      $products = array(
    
        'paper' => array(
    
          'copier' => "Copier & Multipurpose",
          'inkjet' => "Inkjet Printer",
          'laser'  => "Laser Printer",
          'photo'  => "Photographic Paper"),
    
        'pens' => array(
    
          'ball'   => "Ball Point",
          'hilite' => "Highlighters",
          'marker' => "Markers"),
    
        'misc' => array(
    
          'tape'   => "Sticky Tape",
          'glue'   => "Adhesives",
          'clips'  => "Paperclips"
        )
      );
    
      echo "<pre>";
    
      foreach($products as $section => $items)
        foreach($items as $key => $value)
          echo "$section:	$key	($value)<br>";
    
      echo "</pre>";
    ?>
    <?php
    
    // This example is corrected from the one in the book,
    // which had two extra blank rows in the center.
    
    $chessboard = array(
        array('r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'),
        array('p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'),
        array(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '),
        array(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '),
        array(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '),
        array(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '),
        array('P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'),
        array('R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R')
      );
    
      echo "<pre>";
    
      foreach($chessboard as $row)
      {
        foreach ($row as $piece)
          echo "$piece ";
    
        echo "<br>";
      }
    
      echo "</pre>";
    ?>
    <?php
      $temp = explode(' ', "This is a sentence with seven words");
      print_r($temp);
    ?>
    <?php
      $temp = explode('***', "A***sentence***with***asterisks");
      print_r($temp);
    ?>
    <?php
      $fname         = "Doctor";
      $sname         = "Who";
      $planet        = "Gallifrey";
      $system        = "Gridlock";
      $constellation = "Kasterborous";
    
      $contact = compact('fname', 'sname', 'planet', 'system', 'constellation');
    
      print_r($contact);
    ?>
    <?php
      $j       = 23;
      $temp    = "Hello";
      $address = "1 Old Street";
      $age     = 61;
    
      print_r(compact(explode(' ', 'j temp address age')));
    ?>
    <?php
      $paper[] = "Copier";
      $paper[] = "Inkjet";
      $paper[] = "Laser";
      $paper[] = "Photo";
    
      print_r($paper);
    ?>
    <?php
      $paper[0] = "Copier";
      $paper[1] = "Inkjet";
      $paper[2] = "Laser";
      $paper[3] = "Photo";
    
      print_r($paper);
    ?>
    <?php
      $paper[] = "Copier";
      $paper[] = "Inkjet";
      $paper[] = "Laser";
      $paper[] = "Photo";
    
      for ($j = 0 ; $j < 4 ; ++$j)
        echo "$j: $paper[$j]<br>";
    ?>
  • 相关阅读:
    Jzoj4822 完美标号
    Jzoj4822 完美标号
    Jzoj4792 整除
    Jzoj4792 整除
    Educational Codeforces Round 79 A. New Year Garland
    Good Bye 2019 C. Make Good
    ?Good Bye 2019 B. Interesting Subarray
    Good Bye 2019 A. Card Game
    力扣算法题—088扰乱字符串【二叉树】
    力扣算法题—086分隔链表
  • 原文地址:https://www.cnblogs.com/tszr/p/12380909.html
Copyright © 2020-2023  润新知