language/types/array.xml
e587d0655e426f97b3fcb431453da5030e743b23
...
...
@@ -60,16 +60,18 @@ array(
60
60
<programlisting role="php">
61
61
<![CDATA[
62
62
<?php
63
-
$array = array(
63
+
$array1 = array(
64
64
"foo" => "bar",
65
65
"bar" => "foo",
66
66
);
67
67

68
68
// Using the short array syntax
69
-
$array = [
69
+
$array2 = [
70
70
"foo" => "bar",
71
71
"bar" => "foo",
72
72
];
73
+

74
+
var_dump($array1, $array2);
73
75
?>
74
76
]]>
75
77
</programlisting>
...
...
@@ -102,8 +104,8 @@ $array = [
102
104
<listitem>
103
105
<simpara>
104
106
<type>Bool</type>s are cast to <type>int</type>s, too, i.e. the key
105
-
<literal>true</literal> will actually be stored under <literal>1</literal>
106
-
and the key <literal>false</literal> under <literal>0</literal>.
107
+
&true; will actually be stored under <literal>1</literal>
108
+
and the key &false; under <literal>0</literal>.
107
109
</simpara>
108
110
</listitem>
109
111
<listitem>
...
...
@@ -316,6 +318,57 @@ array(7) {
316
318
]]>
317
319
</screen>
318
320
</example>
321
+

322
+
<example>
323
+
<title>Negative index example</title>
324
+
<simpara>
325
+
When assigning a negative integer key <literal>n</literal>, PHP will take care to
326
+
assign the next key to <literal>n+1</literal>.
327
+
</simpara>
328
+
<programlisting role="php">
329
+
<![CDATA[
330
+
<?php
331
+
$array = [];
332
+

333
+
$array[-5] = 1;
334
+
$array[] = 2;
335
+

336
+
var_dump($array);
337
+
?>
338
+
]]>
339
+
</programlisting>
340
+
&example.outputs;
341
+
<screen>
342
+
<![CDATA[
343
+
array(2) {
344
+
[-5]=>
345
+
int(1)
346
+
[-4]=>
347
+
int(2)
348
+
}
349
+
]]>
350
+
</screen>
351
+

352
+
<warning>
353
+
<simpara>
354
+
Prior to PHP 8.3.0, assigning a negative integer key <literal>n</literal> would
355
+
assign the next key to <literal>0</literal>, the previous example would
356
+
therefore output:
357
+
</simpara>
358
+
<informalexample>
359
+
<screen>
360
+
<![CDATA[
361
+
array(2) {
362
+
[-5]=>
363
+
int(1)
364
+
[0]=>
365
+
int(2)
366
+
}
367
+
]]>
368
+
</screen>
369
+
</informalexample>
370
+
</warning>
371
+
</example>
319
372
</sect3>
320
373

321
374
<sect3 xml:id="language.types.array.syntax.accessing">
...
...
@@ -376,8 +429,7 @@ function getArray() {
376
429

377
430
$secondElement = getArray()[1];
378
431

379
-
// or
380
-
list(, $secondElement) = getArray();
432
+
var_dump($secondElement);
381
433
?>
382
434
]]>
383
435
</programlisting>
...
...
@@ -387,7 +439,8 @@ list(, $secondElement) = getArray();
387
439
<para>
388
440
Attempting to access an array key which has not been defined is
389
441
the same as accessing any other undefined variable:
390
-
an <constant>E_NOTICE</constant>-level error message will be
442
+
an <constant>E_WARNING</constant>-level error message
443
+
(<constant>E_NOTICE</constant>-level prior to PHP 8.0.0) will be
391
444
issued, and the result will be &null;.
392
445
</para>
393
446
</note>
...
...
@@ -422,7 +475,7 @@ $arr[] = <replaceable>value</replaceable>;
422
475
// <replaceable>value</replaceable> may be any value of any type</synopsis>
423
476

424
477
<para>
425
-
If <varname>$arr</varname> doesn't exist yet, it will be created, so this is
478
+
If <varname>$arr</varname> doesn't exist yet or is set to &null; or &false;, it will be created, so this is
426
479
also an alternative way to create an <type>array</type>. This practice is
427
480
however discouraged because if <varname>$arr</varname> already contains
428
481
some value (e.g. <type>string</type> from request variable) then this
...
...
@@ -437,6 +490,12 @@ $arr[] = <replaceable>value</replaceable>;
437
490
error. Formerly, the string was silently converted to an array.
438
491
</simpara>
439
492
</note>
493
+
<note>
494
+
<simpara>
495
+
As of PHP 8.1.0, creating a new array from &false; value is deprecated.
496
+
Creating a new array from &null; and undefined values is still allowed.
497
+
</simpara>
498
+
</note>
440
499

441
500
<para>
442
501
To change a certain
...
...
@@ -444,7 +503,8 @@ $arr[] = <replaceable>value</replaceable>;
444
503
key/value pair, call the <function>unset</function> function on it.
445
504
</para>
446
505

447
-
<informalexample>
506
+
<example>
507
+
<title>Using Square Brackets with Arrays</title>
448
508
<programlisting role="php">
449
509
<![CDATA[
450
510
<?php
...
...
@@ -458,11 +518,15 @@ $arr["x"] = 42; // This adds a new element to
458
518

459
519
unset($arr[5]); // This removes the element from the array
460
520

521
+
var_dump($arr);
522
+

461
523
unset($arr); // This deletes the whole array
524
+

525
+
var_dump($arr);
462
526
?>
463
527
]]>
464
528
</programlisting>
465
-
</informalexample>
529
+
</example>
466
530

467
531
<note>
468
532
<para>
...
...
@@ -534,6 +598,144 @@ Array
534
598
</note>
535
599

536
600
</sect3>
601
+

602
+
<sect3 xml:id="language.types.array.syntax.destructuring">
603
+
<title>Array destructuring</title>
604
+

605
+
<para>
606
+
Arrays can be destructured using the <literal>[]</literal> (as of PHP 7.1.0) or
607
+
<function>list</function> language constructs. These
608
+
constructs can be used to destructure an array into distinct variables.
609
+
</para>
610
+

611
+
<example>
612
+
<title>Array Destructuring</title>
613
+
<programlisting role="php">
614
+
<![CDATA[
615
+
<?php
616
+
$source_array = ['foo', 'bar', 'baz'];
617
+

618
+
[$foo, $bar, $baz] = $source_array;
619
+

620
+
echo $foo, PHP_EOL; // prints "foo"
621
+
echo $bar, PHP_EOL; // prints "bar"
622
+
echo $baz, PHP_EOL; // prints "baz"
623
+
?>
624
+
]]>
625
+
</programlisting>
626
+
</example>
627
+

628
+
<para>
629
+
Array destructuring can be used in &foreach; to destructure
630
+
a multi-dimensional array while iterating over it.
631
+
</para>
632
+

633
+
<example>
634
+
<title>Array Destructuring in Foreach</title>
635
+
<programlisting role="php">
636
+
<![CDATA[
637
+
<?php
638
+
$source_array = [
639
+
[1, 'John'],
640
+
[2, 'Jane'],
641
+
];
642
+

643
+
foreach ($source_array as [$id, $name]) {
644
+
echo "{$id}: '{$name}'\n";
645
+
}
646
+
?>
647
+
]]>
648
+
</programlisting>
649
+
</example>
650
+

651
+
<para>
652
+
Array elements will be ignored if the variable is not provided. Array
653
+
destructuring always starts at index <literal>0</literal>.
654
+
</para>
655
+

656
+
<example>
657
+
<title>Ignoring Elements</title>
658
+
<programlisting role="php">
659
+
<![CDATA[
660
+
<?php
661
+
$source_array = ['foo', 'bar', 'baz'];
662
+

663
+
// Assign the element at index 2 to the variable $baz
664
+
[, , $baz] = $source_array;
665
+

666
+
echo $baz; // prints "baz"
667
+
?>
668
+
]]>
669
+
</programlisting>
670
+
</example>
671
+

672
+
<para>
673
+
As of PHP 7.1.0, associative arrays can be destructured too. This also
674
+
allows for easier selection of the right element in numerically indexed
675
+
arrays as the index can be explicitly specified.
676
+
</para>
677
+

678
+
<example>
679
+
<title>Destructuring Associative Arrays</title>
680
+
<programlisting role="php">
681
+
<![CDATA[
682
+
<?php
683
+
$source_array = ['foo' => 1, 'bar' => 2, 'baz' => 3];
684
+

685
+
// Assign the element at index 'baz' to the variable $three
686
+
['baz' => $three] = $source_array;
687
+

688
+
echo $three, PHP_EOL; // prints 3
689
+

690
+
$source_array = ['foo', 'bar', 'baz'];
691
+

692
+
// Assign the element at index 2 to the variable $baz
693
+
[2 => $baz] = $source_array;
694
+

695
+
echo $baz, PHP_EOL; // prints "baz"
696
+
?>
697
+
]]>
698
+
</programlisting>
699
+
</example>
700
+

701
+
<para>
702
+
Array destructuring can be used for easy swapping of two variables.
703
+
</para>
704
+

705
+
<example>
706
+
<title>Swapping Two Variable</title>
707
+
<programlisting role="php">
708
+
<![CDATA[
709
+
<?php
710
+
$a = 1;
711
+
$b = 2;
712
+

713
+
[$b, $a] = [$a, $b];
714
+

715
+
echo $a, PHP_EOL; // prints 2
716
+
echo $b, PHP_EOL; // prints 1
717
+
?>
718
+
]]>
719
+
</programlisting>
720
+
</example>
721
+

722
+
<note>
723
+
<para>
724
+
The spread operator (<literal>...</literal>) is not supported in assignments.
725
+
</para>
726
+
</note>
727
+

728
+
<note>
729
+
<para>
730
+
Attempting to access an array key which has not been defined is
731
+
the same as accessing any other undefined variable:
732
+
an <constant>E_WARNING</constant>-level error message
733
+
(<constant>E_NOTICE</constant>-level prior to PHP 8.0.0) will be
734
+
issued, and the result will be &null;.
735
+
</para>
736
+
</note>
737
+
</sect3>
738
+

537
739
</sect2><!-- end syntax -->
538
740

539
741
<sect2 xml:id="language.types.array.useful-funcs">
...
...
@@ -553,24 +755,28 @@ Array
553
755
<function>array_values</function> function.
554
756
</para>
555
757

556
-
<informalexample>
758
+
<example>
759
+
<title>Unsetting Intermediate Elements</title>
557
760
<programlisting role="php">
558
761
<![CDATA[
559
762
<?php
560
763
$a = array(1 => 'one', 2 => 'two', 3 => 'three');
561
-
unset($a[2]);
764
+

562
765
/* will produce an array that would have been defined as
563
766
$a = array(1 => 'one', 3 => 'three');
564
767
and NOT
565
768
$a = array(1 => 'one', 2 =>'three');
566
769
*/
770
+
unset($a[2]);
771
+
var_dump($a);
567
772

568
773
$b = array_values($a);
569
774
// Now $b is array(0 => 'one', 1 =>'three')
775
+
var_dump($b);
570
776
?>
571
777
]]>
572
778
</programlisting>
573
-
</informalexample>
779
+
</example>
574
780
</note>
575
781

576
782
<para>
...
...
@@ -626,24 +832,26 @@ echo $foo[bar];
626
832
</simpara>
627
833
</warning>
628
834

629
-
<note>
630
-
<simpara>
631
-
This does not mean to <emphasis>always</emphasis> quote the key. Do not
632
-
quote keys which are <link linkend="language.constants">constants</link> or
633
-
<link linkend="language.variables">variables</link>, as this will prevent
634
-
PHP from interpreting them.
635
-
</simpara>
835
+
<simpara>
836
+
This does not mean to <emphasis>always</emphasis> quote the key. Do not
837
+
quote keys which are <link linkend="language.constants">constants</link> or
838
+
<link linkend="language.variables">variables</link>, as this will prevent
839
+
PHP from interpreting them.
840
+
</simpara>
636
841

637
-
<informalexample>
638
-
<programlisting role="php">
842
+
<example>
843
+
<title>Key Quoting</title>
844
+
<programlisting role="php">
639
845
<![CDATA[
640
846
<?php
641
847
error_reporting(E_ALL);
642
848
ini_set('display_errors', true);
643
849
ini_set('html_errors', false);
850
+

644
851
// Simple array:
645
852
$array = array(1, 2);
646
853
$count = count($array);
854
+

647
855
for ($i = 0; $i < $count; $i++) {
648
856
echo "\nChecking $i: \n";
649
857
echo "Bad: " . $array['$i'] . "\n";
...
...
@@ -654,7 +862,7 @@ for ($i = 0; $i < $count; $i++) {
654
862
?>
655
863
]]>
656
864
</programlisting>
657
-
</informalexample>
865
+
</example>
658
866
&example.outputs;
659
867
<screen>
660
868
<![CDATA[
...
...
@@ -674,14 +882,14 @@ Notice: Undefined index: $i in /path/to/script.html on line 11
674
882
Bad:
675
883
Good: 2
676
884
]]>
677
-
</screen>
678
-
</note>
885
+
</screen>
679
886

680
887
<para>
681
888
More examples to demonstrate this behaviour:
682
889
</para>
683
890

684
-
<informalexample>
891
+
<example>
892
+
<title>More Examples</title>
685
893
<programlisting role="php">
686
894
<![CDATA[
687
895
<?php
...
...
@@ -691,40 +899,52 @@ error_reporting(E_ALL);
691
899
$arr = array('fruit' => 'apple', 'veggie' => 'carrot');
692
900

693
901
// Correct
694
-
print $arr['fruit']; // apple
695
-
print $arr['veggie']; // carrot
902
+
echo $arr['fruit'], PHP_EOL; // apple
903
+
echo $arr['veggie'], PHP_EOL; // carrot
696
904

697
-
// Incorrect. This works but also throws a PHP error of level E_NOTICE because
905
+
// Incorrect. This works but also throws a PHP Error because
698
906
// of an undefined constant named fruit
699
907
//
700
-
// Notice: Use of undefined constant fruit - assumed 'fruit' in...
701
-
print $arr[fruit]; // apple
908
+
// Error: Undefined constant "fruit"
909
+
try {
910
+
echo $arr[fruit]; // apple
911
+
} catch (Error $e) {
912
+
echo get_class($e), ': ', $e->getMessage(), PHP_EOL;
913
+
}
702
914

703
915
// This defines a constant to demonstrate what's going on. The value 'veggie'
704
916
// is assigned to a constant named fruit.
705
917
define('fruit', 'veggie');
706
918

707
919
// Notice the difference now
708
-
print $arr['fruit']; // apple
709
-
print $arr[fruit]; // carrot
920
+
echo $arr['fruit'], PHP_EOL; // apple
921
+
echo $arr[fruit], PHP_EOL; // carrot
710
922

711
923
// The following is okay, as it's inside a string. Constants are not looked for
712
924
// within strings, so no E_NOTICE occurs here
713
-
print "Hello $arr[fruit]"; // Hello apple
925
+
echo "Hello $arr[fruit]", PHP_EOL; // Hello apple
714
926

715
927
// With one exception: braces surrounding arrays within strings allows constants
716
928
// to be interpreted
717
-
print "Hello {$arr[fruit]}"; // Hello carrot
718
-
print "Hello {$arr['fruit']}"; // Hello apple
929
+
echo "Hello {$arr[fruit]}", PHP_EOL; // Hello carrot
930
+
echo "Hello {$arr['fruit']}", PHP_EOL; // Hello apple
719
931

932
+
// Concatenation is another option
933
+
echo "Hello " . $arr['fruit'], PHP_EOL; // Hello apple
934
+
?>
935
+
]]>
936
+
</programlisting>
937
+
</example>
938
+

939
+
<informalexample>
940
+
<programlisting role="php">
941
+
<![CDATA[
942
+
<?php
720
943
// This will not work, and will result in a parse error, such as:
721
944
// Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING'
722
945
// This of course applies to using superglobals in strings as well
723
946
print "Hello $arr['fruit']";
724
947
print "Hello $_GET['foo']";
725
-

726
-
// Concatenation is another option
727
-
print "Hello " . $arr['fruit']; // Hello apple
728
948
?>
729
949
]]>
730
950
</programlisting>
...
...
@@ -828,7 +1048,7 @@ $error_descriptions[8] = "This is just an informal notice";
828
1048
<type>string</type>, <type>bool</type> and <type>resource</type>,
829
1049
converting a value to an <type>array</type> results in an array with a single
830
1050
element with index zero and the value of the scalar which was converted. In
831
-
other words, <literal>(array)$scalarValue</literal> is exactly the same as
1051
+
other words, <code>(array) $scalarValue</code> is exactly the same as
832
1052
<literal>array($scalarValue)</literal>.
833
1053
</para>
834
1054

...
...
@@ -844,7 +1064,8 @@ $error_descriptions[8] = "This is just an informal notice";
844
1064
are silently discarded.
845
1065
</para>
846
1066

847
-
<informalexample>
1067
+
<example>
1068
+
<title>Converting to an Array</title>
848
1069
<programlisting role="php">
849
1070
<![CDATA[
850
1071
<?php
...
...
@@ -874,13 +1095,14 @@ array (
874
1095
)
875
1096
]]>
876
1097
</screen>
877
-
</informalexample>
1098
+
</example>
878
1099

879
1100
<para>
880
1101
These <literal>NUL</literal> can result in some unexpected behaviour:
881
1102
</para>
882
1103

883
-
<informalexample>
1104
+
<example>
1105
+
<title>Casting an Object to an Array</title>
884
1106
<programlisting role="php">
885
1107
<![CDATA[
886
1108
<?php
...
...
@@ -911,7 +1133,7 @@ array(3) {
911
1133
}
912
1134
]]>
913
1135
</screen>
914
-
</informalexample>
1136
+
</example>
915
1137

916
1138
<para>
917
1139
The above will appear to have two keys named 'AA', although one of them is
...
...
@@ -934,6 +1156,102 @@ array(3) {
934
1156
</para>
935
1157
</sect2>
936
1158

1159
+
<sect2 xml:id="language.types.array.unpacking">
1160
+
<title>Array unpacking</title>
1161
+

1162
+
<para>
1163
+
An array prefixed by <code>...</code> will be expanded in place during array definition.
1164
+
Only arrays and objects which implement <interfacename>Traversable</interfacename> can be expanded.
1165
+
Array unpacking with <code>...</code> is available as of PHP 7.4.0.
1166
+
</para>
1167
+

1168
+
<para>
1169
+
It's possible to expand multiple times, and add normal elements before or after the <code>...</code> operator:
1170
+

1171
+
<example>
1172
+
<title>Simple array unpacking</title>
1173
+
<programlisting role="php">
1174
+
<![CDATA[
1175
+
<?php
1176
+
// Using short array syntax.
1177
+
// Also, works with array() syntax.
1178
+
$arr1 = [1, 2, 3];
1179
+
$arr2 = [...$arr1]; // [1, 2, 3]
1180
+
$arr3 = [0, ...$arr1]; // [0, 1, 2, 3]
1181
+
$arr4 = [...$arr1, ...$arr2, 111]; // [1, 2, 3, 1, 2, 3, 111]
1182
+
$arr5 = [...$arr1, ...$arr1]; // [1, 2, 3, 1, 2, 3]
1183
+

1184
+
function getArr() {
1185
+
return ['a', 'b'];
1186
+
}
1187
+
$arr6 = [...getArr(), 'c' => 'd']; // ['a', 'b', 'c' => 'd']
1188
+

1189
+
var_dump($arr1, $arr2, $arr3, $arr4, $arr5, $arr6);
1190
+
?>
1191
+
]]>
1192
+
</programlisting>
1193
+
</example>
1194
+
</para>
1195
+

1196
+
<para>
1197
+
Unpacking an array with the <code>...</code> operator follows the semantics of the <function>array_merge</function> function.
1198
+
That is, later string keys overwrite earlier ones and integer keys are renumbered:
1199
+

1200
+
<example>
1201
+
<title>Array unpacking with duplicate key</title>
1202
+
<programlisting role="php">
1203
+
<![CDATA[
1204
+
<?php
1205
+
// string key
1206
+
$arr1 = ["a" => 1];
1207
+
$arr2 = ["a" => 2];
1208
+
$arr3 = ["a" => 0, ...$arr1, ...$arr2];
1209
+
var_dump($arr3); // ["a" => 2]
1210
+

1211
+
// integer key
1212
+
$arr4 = [1, 2, 3];
1213
+
$arr5 = [4, 5, 6];
1214
+
$arr6 = [...$arr4, ...$arr5];
1215
+
var_dump($arr6); // [1, 2, 3, 4, 5, 6]
1216
+
// Which is [0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5, 5 => 6]
1217
+
// where the original integer keys have not been retained.
1218
+
?>
1219
+
]]>
1220
+
</programlisting>
1221
+
</example>
1222
+
</para>
1223
+

1224
+
<note>
1225
+
<para>
1226
+
Keys that are neither integers nor strings throw a <classname>TypeError</classname>.
1227
+
Such keys can only be generated by a <interfacename>Traversable</interfacename> object.
1228
+
</para>
1229
+
</note>
1230
+
<note>
1231
+
<para>
1232
+
Prior to PHP 8.1, unpacking an array which has a string key is not supported:
1233
+
</para>
1234
+
<informalexample>
1235
+
<programlisting role="php">
1236
+
<![CDATA[
1237
+
<?php
1238
+

1239
+
$arr1 = [1, 2, 3];
1240
+
$arr2 = ['a' => 4];
1241
+
$arr3 = [...$arr1, ...$arr2];
1242
+
// Fatal error: Uncaught Error: Cannot unpack array with string keys in example.php:5
1243
+

1244
+
$arr4 = [1, 2, 3];
1245
+
$arr5 = [4, 5];
1246
+
$arr6 = [...$arr4, ...$arr5]; // works. [1, 2, 3, 4, 5]
1247
+
?>
1248
+
]]>
1249
+
</programlisting>
1250
+
</informalexample>
1251
+
</note>
1252
+

1253
+
</sect2>
1254
+

937
1255
<sect2 xml:id="language.types.array.examples">
938
1256
<title>Examples</title>
939
1257

...
...
@@ -941,7 +1259,8 @@ array(3) {
941
1259
The array type in PHP is very versatile. Here are some examples:
942
1260
</para>
943
1261

944
-
<informalexample>
1262
+
<example>
1263
+
<title>Array Versatility</title>
945
1264
<programlisting role="php">
946
1265
<![CDATA[
947
1266
<?php
...
...
@@ -955,6 +1274,8 @@ $a = array( 'color' => 'red',
955
1274

956
1275
$b = array('a', 'b', 'c');
957
1276

1277
+
var_dump($a, $b);
1278
+

958
1279
// . . .is completely equivalent with this:
959
1280
$a = array();
960
1281
$a['color'] = 'red';
...
...
@@ -972,10 +1293,12 @@ $b[] = 'c';
972
1293
// array('color' => 'red', 'taste' => 'sweet', 'shape' => 'round',
973
1294
// 'name' => 'apple', 0 => 4), and $b will be the array
974
1295
// array(0 => 'a', 1 => 'b', 2 => 'c'), or simply array('a', 'b', 'c').
1296
+

1297
+
var_dump($a, $b);
975
1298
?>
976
1299
]]>
977
1300
</programlisting>
978
-
</informalexample>
1301
+
</example>
979
1302

980
1303
<example>
981
1304
<title>Using array()</title>
...
...
@@ -988,15 +1311,17 @@ $map = array( 'version' => 4,
988
1311
'lang' => 'english',
989
1312
'short_tags' => true
990
1313
);
1314
+
var_dump($map);
991
1315

992
1316
// strictly numerical keys
1317
+
// this is the same as array(0 => 7, 1 => 8, ...)
993
1318
$array = array( 7,
994
1319
8,
995
1320
0,
996
1321
156,
997
1322
-10
998
1323
);
999
-
// this is the same as array(0 => 7, 1 => 8, ...)
1324
+
var_dump($array);
1000
1325

1001
1326
$switching = array( 10, // key = 0
1002
1327
5 => 6,
...
...
@@ -1007,9 +1332,11 @@ $switching = array( 10, // key = 0
1007
1332
'02' => 77, // key = '02'
1008
1333
0 => 12 // the value 10 will be overwritten by 12
1009
1334
);
1335
+
var_dump($switching);
1010
1336

1011
1337
// empty array
1012
1338
$empty = array();
1339
+
var_dump($empty);
1013
1340
?>
1014
1341
]]>
1015
1342
<!-- TODO example of
...
...
@@ -1055,8 +1382,10 @@ Do you like yellow?
1055
1382
<programlisting role="php">
1056
1383
<![CDATA[
1057
1384
<?php
1385
+
$colors = array('red', 'blue', 'green', 'yellow');
1386
+

1058
1387
foreach ($colors as &$color) {
1059
-
$color = strtoupper($color);
1388
+
$color = mb_strtoupper($color);
1060
1389
}
1061
1390
unset($color); /* ensure that following writes to
1062
1391
$color will not modify the last array element */
...
...
@@ -1088,7 +1417,7 @@ Array
1088
1417
<programlisting role="php">
1089
1418
<![CDATA[
1090
1419
<?php
1091
-
$firstquarter = array(1 => 'January', 'February', 'March');
1420
+
$firstquarter = array(1 => 'January', 'February', 'March');
1092
1421
print_r($firstquarter);
1093
1422
?>
1094
1423
]]>
...
...
@@ -1098,9 +1427,9 @@ print_r($firstquarter);
1098
1427
<![CDATA[
1099
1428
Array
1100
1429
(
1101
-
[1] => 'January'
1102
-
[2] => 'February'
1103
-
[3] => 'March'
1430
+
[1] => January
1431
+
[2] => February
1432
+
[3] => March
1104
1433
)
1105
1434
]]>
1106
1435
</screen>
...
...
@@ -1117,6 +1446,8 @@ while (false !== ($file = readdir($handle))) {
1117
1446
$files[] = $file;
1118
1447
}
1119
1448
closedir($handle);
1449
+

1450
+
var_dump($files);
1120
1451
?>
1121
1452
]]>
1122
1453
</programlisting>
...
...
@@ -1131,7 +1462,7 @@ closedir($handle);
1131
1462

1132
1463
<example>
1133
1464
<title>Sorting an array</title>
1134
-
<programlisting role="php">
1465
+
<programlisting role="php" annotations="non-interactive">
1135
1466
<![CDATA[
1136
1467
<?php
1137
1468
sort($files);
...
...
@@ -1168,6 +1499,7 @@ $fruits = array ( "fruits" => array ( "a" => "orange",
1168
1499
"third"
1169
1500
)
1170
1501
);
1502
+
var_dump($fruits);
1171
1503

1172
1504
// Some examples to address values in the array above
1173
1505
echo $fruits["holes"][5]; // prints "second"
...
...
@@ -1176,6 +1508,7 @@ unset($fruits["holes"][0]); // remove "first"
1176
1508

1177
1509
// Create a new multi-dimensional array
1178
1510
$juices["apple"]["green"] = "good";
1511
+
var_dump($juices);
1179
1512
?>
1180
1513
]]>
1181
1514
</programlisting>
...
...
@@ -1187,7 +1520,8 @@ $juices["apple"]["green"] = "good";
1187
1520
<type>array</type> by reference.
1188
1521
</para>
1189
1522

1190
-
<informalexample>
1523
+
<example>
1524
+
<title>Array Copying</title>
1191
1525
<programlisting role="php">
1192
1526
<![CDATA[
1193
1527
<?php
...
...
@@ -1198,10 +1532,12 @@ $arr2[] = 4; // $arr2 is changed,
1198
1532

1199
1533
$arr3 = &$arr1;
1200
1534
$arr3[] = 4; // now $arr1 and $arr3 are the same
1535
+

1536
+
var_dump($arr1, $arr2, $arr3);
1201
1537
?>
1202
1538
]]>
1203
1539
</programlisting>
1204
-
</informalexample>
1540
+
</example>
1205
1541

1206
1542
</sect2>
1207
1543
</sect1>
1208
1544