⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sdo.sample.getset.html

📁 php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Setting and Getting Property Values</title>  <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="sdo.examples.html">Examples</a></div> <div class="next" style="text-align: right; float: right;"><a href="sdo.sample.sequence.html">Working with Sequenced Data Objects</a></div> <div class="up"><a href="sdo.examples.html">Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="sdo.sample.getset" class="section">  <h2 class="title">Setting and Getting Property Values</h2>  <p class="para"> The following examples assume   <strong class="command">$company</strong> is the root of a tree of data objects created from the   schema and instance document shown above.  </p>  <p class="para">   <div class="example">    <p><b>Example #1 Access via property name</b></p>    <div class="example-contents"><p>     Data object properties can be accessed using the object property     access syntax.  The following sets the company name to &#039;Acme&#039;.    </p></div>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$company</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'Acme'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>  </p>  <p class="para">   <div class="example">    <p><b>Example #2 Access via property name as array index</b></p>    <div class="example-contents"><p>We can also access properties using associative array syntax. The simplest     form of this uses the property name as the array index. For example, the following sets     the company name and gets the employeeOfTheMonth.    </p></div>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$company</span><span style="color: #007700">[</span><span style="color: #DD0000">'name'</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">'UltraCorp'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$eotm&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$company</span><span style="color: #007700">[</span><span style="color: #DD0000">'employeeOfTheMonth'</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>  </p>  <p class="para">   <div class="example">    <p><b>Example #3 Data Object iteration</b></p>    <div class="example-contents"><p>     We can iterate over the properties of a data object using foreach.     The following iterates over the properties of the employee of the month.    </p></div>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;$eotm&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$company</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">employeeOfTheMonth</span><span style="color: #007700">;<br />&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">$eotm&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$name&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"$name:&nbsp;$value\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>    <div class="example-contents"><p>      which will output:    </p></div>         <div class="example-contents"><div class="cdata"><pre>name: Jane DoeSN: E0003</pre></div>    </div>    <div class="example-contents"><p>      The &#039;manager&#039; property is not output, because it has not been set.    </p></div>   </div>  </p>    <p class="para">   <div class="example">    <p><b>Example #4 Access many-valued property by name</b></p>    <div class="example-contents"><p> Many-valued data object properties can also be accessed using the object     property name syntax. The following gets the list of departments.    </p></div>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$departments&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$company</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">departments</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>  </p>    <p class="para">   <div class="example">    <p><b>Example #5 Many-valued element access</b></p>    <div class="example-contents"><p>     We can access individual elements of many-valued properties using array     syntax. The following accesses the first department in the company.    </p></div>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$ad_tech_dept&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$company</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">departments</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>  </p>  <p class="para">   <div class="example">    <p><b>Example #6 Many-valued property iteration</b></p>    <div class="example-contents"><p>     Many-valued properties can also be iterated over using     foreach.  The following iterates over the company&#039;s departments.    </p></div>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;</span><span style="color: #007700">foreach&nbsp;(</span><span style="color: #0000BB">$company</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">departments&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$department</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;...<br />&nbsp;&nbsp;</span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>    <div class="example-contents"><p>     Each iteration will assign the next department in the     list to the variable <strong class="command">$department</strong>.    </p></div>   </div>  </p>  <p class="para">   <div class="example">    <p><b>Example #7 Chained property access</b></p>    <div class="example-contents"><p> We can chain property references on a single line.      The following sets and gets the name of the first department.    </p></div>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;$company</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">departments</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'Emerging&nbsp;Technologies'</span><span style="color: #007700">;<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$dept_name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$company</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">departments</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">name</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>    <div class="example-contents"><p>Using the associative array syntax, this is equivalent to</p></div>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;$company</span><span style="color: #007700">[</span><span style="color: #DD0000">'departments'</span><span style="color: #007700">][</span><span style="color: #0000BB">0</span><span style="color: #007700">][</span><span style="color: #DD0000">'name'</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">'Emerging&nbsp;Technologies'</span><span style="color: #007700">;<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$dept_name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$company</span><span style="color: #007700">[</span><span style="color: #DD0000">'departments'</span><span style="color: #007700">][</span><span style="color: #0000BB">0</span><span style="color: #007700">][</span><span style="color: #DD0000">'name'</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>    <div class="example-contents"><p> In either case, the dept_name variable is set to &#039;Emerging Technologies&#039;.    </p></div>   </div>  </p>  <p class="para">

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -