svn: /phpdoc/en/trunk/reference/misc/functions/ unpack.xml

From: Date: Thu, 10 Feb 2011 12:36:37 +0000
Subject: svn: /phpdoc/en/trunk/reference/misc/functions/ unpack.xml
Groups: php.doc.cvs 
Request: Send a blank email to doc-cvs+get-7727@lists.php.net to get a copy of this message
derick Thu, 10 Feb 2011 12:36:37 +0000 Revision: http://svn.php.net/viewvc?view=revision&revision=308185 Log: Update pack() documentation. Changed paths: U phpdoc/en/trunk/reference/misc/functions/unpack.xml Modified: phpdoc/en/trunk/reference/misc/functions/unpack.xml =================================================================== --- phpdoc/en/trunk/reference/misc/functions/unpack.xml 2011-02-10 12:31:09 UTC (rev 308184) +++ phpdoc/en/trunk/reference/misc/functions/unpack.xml 2011-02-10 12:36:37 UTC (rev 308185) @@ -18,10 +18,11 @@ <parameter>format</parameter>. </para> <para> - <function>unpack</function> works slightly different from Perl as - the unpacked data is stored in an associative array. To + The unpacked data is stored in an associative array. To accomplish this you have to name the different format codes and - separate them by a slash /. + separate them by a slash /. If a repeater argument is present, + then each of the array keys will have a sequence number behind + the given name. </para> </refsect1> @@ -65,18 +66,36 @@ <programlisting role="php"> <![CDATA[ <?php -$array = unpack("c2chars/nint", $binarydata); +$binarydata = "\0x04\0x00\0xa0 +$array = unpack("cchar/nint", $binarydata); ?> ]]> </programlisting> <para> - The resulting array will contain the entries "chars1", - "chars2" and "int". + The resulting array will contain the entries "chars" with value + <literal>4</literal> and "int" with <literal>160</literal>. </para> </example> </para> - </refsect1> + <para> + <example> + <title><function>unpack</function> example with a repeater</title> + <programlisting role="php"> + <![CDATA[ + <?php + $array = unpack("c2chars/nint", $binarydata); + ?> + ]]> + </programlisting> + <para> + The resulting array will contain the entries "chars1", + "chars2" and "int". + </para> + </example> + </para> + </refsect1> + <refsect1 role="notes"> &reftitle.notes; <caution> @@ -87,6 +106,34 @@ even though unsigned unpacking was specified. </para> </caution> + <caution> + <para> + Be aware that if you do not name an element, an empty string is used. + If you do not name more than one element, this means + that some data is overwritten as the keys are the same such as in: + </para> + <para> + <example> + <title><function>unpack</function> example with unnamed keys</title> + <programlisting role="php"> +<![CDATA[ +<?php +$binarydata = "\x32\x42\x00\xa0"; +$array = unpack("c2/n", $binarydata); +var_dump($array); +?> +]]> + </programlisting> + <para> + The resulting array will contain the entries "1" with value + <literal>160</literal> and "2" with <literal>66</literal>. The + first value from the <literal>c</literal> specifier is + overwritten by the first value from the <literal>n</literal> + specifier. + </para> + </example> + </para> + </caution> </refsect1> <refsect1 role="seealso">

« previous php.doc.cvs (#7727) next »