CVS update: php3/doc/chapters
| From: | cmv | Date: | Tue, 11 May 1999 17:26:38 +0000 |
| Subject: | CVS update: php3/doc/chapters | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-5533@lists.php.net to get a copy of this message | ||
Date: Tuesday May 11, 1999 @ 13:26
Author: cmv
Update of /repository/php3/doc/chapters
In directory php:/tmp/cvs-serv21105/doc/chapters
Modified Files:
lang-syntax.sgml
Log Message:
Added another section on multi-dimensional arrays.
I have no way of testing whether I busted the SGML code for this (sorry).
I'm pretty sure I didn't, but if someone with more knowledge than I can
double check my code, that would be great. Thanks!
Index: php3/doc/chapters/lang-syntax.sgml
diff -u php3/doc/chapters/lang-syntax.sgml:1.34 php3/doc/chapters/lang-syntax.sgml:1.35
--- php3/doc/chapters/lang-syntax.sgml:1.34 Sun May 9 01:48:30 1999
+++ php3/doc/chapters/lang-syntax.sgml Tue May 11 13:26:38 1999
@@ -648,6 +648,9 @@
<sect1 id="variable-arrays">
<title>Array manipulation</title>
+ <sect2 id="variable-arrays.single-dim">
+ <title>Single Dimension Arrays</title>
+
<para>
PHP supports both scalar and associative arrays. In fact, there
is no difference between the two. You can create an array using
@@ -690,6 +693,84 @@
<function>prev</function> functions. Another common way to
traverse an array is to use the <function>each</function>
function.
+
+ <sect2 id="variable-arrays.multi-dim">
+ <title>Multi-Dimensional Arrays</title>
+
+ <para>
+ Multi-dimensional arrays are actually pretty simple. For each
+ dimension of the array, you add another [key] value to the end:
+
+ <informalexample>
+ <programlisting>
+$a[1] = $f; # one dimensional examples
+$a["foo"] = $f;
+
+$a[1][0] = $f; # two dimensional
+$a["foo"][2] = $f; # (you can mix numeric and associative indices)
+$a[3]["bar"] = $f; # (you can mix numeric and associative indices)
+
+$a["foo"][4]["bar"][0] = $f; # four dimensional!
+ </programlisting>
+ </informalexample>
+
+ <para>
+ You can "fill up" multi-dimensional arrays in many ways, but
+ the trickiest one to grok is how to use the <function>array</function>
+ command for associative arrays. These two snippets of code fill up
+ the one-dimensional array in the same way:
+
+ <informalexample>
+ <programlisting>
+# Example 1:
+
+$a["color"] = "red";
+$a["taste"] = "sweet";
+$a["shape"] = "round";
+$a["name"] = "apple";
+$a[3] = 4;
+
+
+# Example 2:
+$a = array(
+ "color" => "red",
+ "taste" => "sweet",
+ "shape" => "round",
+ "name" => "apple",
+ 3 => 4
+);
+ </programlisting>
+ </informalexample>
+
+ <para>
+ The <function>array</function> function can be nested for
+ multi-dimensional arrays:
+
+ <informalexample>
+ <programlisting>
+<?
+$a = array(
+ "apple" => array(
+ "color" => "red",
+ "taste" => "sweet",
+ "shape" => "round"
+ ),
+ "orange" => array(
+ "color" => "orange",
+ "taste" => "sweet",
+ "shape" => "round"
+ ),
+ "banana" => array(
+ "color" => "yellow",
+ "taste" => "paste-y",
+ "shape" => "banana-shaped"
+ )
+);
+
+echo $a["apple"]["taste"]; # will output "sweet"
+?>
+ </programlisting>
+ </informalexample>
</chapter>
--
PHP Development Mailing List http://www.php.net/
To unsubscribe send an empty message to php-dev-unsubscribe@lists.php.net
For help: php-dev-help@lists.php.net