1--TEST--
2serialization: arrays with references, nested
3--FILE--
4<?php
5/* Prototype  : proto string serialize(mixed variable)
6 * Description: Returns a string representation of variable (which can later be unserialized)
7 * Source code: ext/standard/var.c
8 * Alias to functions:
9 */
10/* Prototype  : proto mixed unserialize(string variable_representation)
11 * Description: Takes a string representation of variable and recreates it
12 * Source code: ext/standard/var.c
13 * Alias to functions:
14 */
15
16function check(&$a) {
17	var_dump($a);
18	$ser = serialize($a);
19	var_dump($ser);
20
21	$b = unserialize($ser);
22
23	// Change each element and dump result.
24	foreach($b as $k=>$v) {
25		if (is_array($v)){
26			foreach($b[$k] as $sk=>$sv) {
27				$b[$k][$sk] = "b$k.$sk.changed";
28				var_dump($b);
29			}
30		} else {
31			$b[$k] = "b$k.changed";
32			var_dump($b);
33		}
34	}
35}
36
37echo "\n\n--- Nested array references 1 element in containing array:\n";
38$a = array();
39$c = array(1,1,&$a);
40$a[0] = &$c[0];
41$a[1] = 1;
42check($c);
43
44echo "\n\n--- Nested array references 1 element in containing array (slightly different):\n";
45$a = array();
46$c = array(1,&$a,1);
47$a[0] = 1;
48$a[1] = &$c[0];
49check($c);
50
51echo "\n\n--- Nested array references 2 elements in containing array:\n";
52$a = array();
53$c = array(1,1,&$a);
54$a[0] = &$c[0];
55$a[1] = &$c[1];
56check($c);
57
58
59echo "\n\n--- Containing array references 1 element in nested array:\n";
60$a = array();
61$a[0] = 1;
62$a[1] = 1;
63$c = array(1,&$a[0],&$a);
64check($c);
65
66echo "\n\n--- Containing array references 2 elements in nested array:\n";
67$a = array();
68$a[0] = 1;
69$a[1] = 1;
70$c = array(&$a[0],&$a[1],&$a);
71check($c);
72
73echo "\n\n--- Nested array references container:\n";
74$a = array();
75$c = array(1,1,&$a);
76$a[0] = 1;
77$a[1] = &$c;
78check($c);
79
80?>
81--EXPECTF--
82--- Nested array references 1 element in containing array:
83array(3) {
84  [0]=>
85  &int(1)
86  [1]=>
87  int(1)
88  [2]=>
89  &array(2) {
90    [0]=>
91    &int(1)
92    [1]=>
93    int(1)
94  }
95}
96string(48) "a:3:{i:0;i:1;i:1;i:1;i:2;a:2:{i:0;R:2;i:1;i:1;}}"
97array(3) {
98  [0]=>
99  &string(10) "b0.changed"
100  [1]=>
101  int(1)
102  [2]=>
103  array(2) {
104    [0]=>
105    &string(10) "b0.changed"
106    [1]=>
107    int(1)
108  }
109}
110array(3) {
111  [0]=>
112  &string(10) "b0.changed"
113  [1]=>
114  string(10) "b1.changed"
115  [2]=>
116  array(2) {
117    [0]=>
118    &string(10) "b0.changed"
119    [1]=>
120    int(1)
121  }
122}
123array(3) {
124  [0]=>
125  &string(12) "b2.0.changed"
126  [1]=>
127  string(10) "b1.changed"
128  [2]=>
129  array(2) {
130    [0]=>
131    &string(12) "b2.0.changed"
132    [1]=>
133    int(1)
134  }
135}
136array(3) {
137  [0]=>
138  &string(12) "b2.0.changed"
139  [1]=>
140  string(10) "b1.changed"
141  [2]=>
142  array(2) {
143    [0]=>
144    &string(12) "b2.0.changed"
145    [1]=>
146    string(12) "b2.1.changed"
147  }
148}
149
150
151--- Nested array references 1 element in containing array (slightly different):
152array(3) {
153  [0]=>
154  &int(1)
155  [1]=>
156  &array(2) {
157    [0]=>
158    int(1)
159    [1]=>
160    &int(1)
161  }
162  [2]=>
163  int(1)
164}
165string(48) "a:3:{i:0;i:1;i:1;a:2:{i:0;i:1;i:1;R:2;}i:2;i:1;}"
166array(3) {
167  [0]=>
168  &string(10) "b0.changed"
169  [1]=>
170  array(2) {
171    [0]=>
172    int(1)
173    [1]=>
174    &string(10) "b0.changed"
175  }
176  [2]=>
177  int(1)
178}
179array(3) {
180  [0]=>
181  &string(10) "b0.changed"
182  [1]=>
183  array(2) {
184    [0]=>
185    string(12) "b1.0.changed"
186    [1]=>
187    &string(10) "b0.changed"
188  }
189  [2]=>
190  int(1)
191}
192array(3) {
193  [0]=>
194  &string(12) "b1.1.changed"
195  [1]=>
196  array(2) {
197    [0]=>
198    string(12) "b1.0.changed"
199    [1]=>
200    &string(12) "b1.1.changed"
201  }
202  [2]=>
203  int(1)
204}
205array(3) {
206  [0]=>
207  &string(12) "b1.1.changed"
208  [1]=>
209  array(2) {
210    [0]=>
211    string(12) "b1.0.changed"
212    [1]=>
213    &string(12) "b1.1.changed"
214  }
215  [2]=>
216  string(10) "b2.changed"
217}
218
219
220--- Nested array references 2 elements in containing array:
221array(3) {
222  [0]=>
223  &int(1)
224  [1]=>
225  &int(1)
226  [2]=>
227  &array(2) {
228    [0]=>
229    &int(1)
230    [1]=>
231    &int(1)
232  }
233}
234string(48) "a:3:{i:0;i:1;i:1;i:1;i:2;a:2:{i:0;R:2;i:1;R:3;}}"
235array(3) {
236  [0]=>
237  &string(10) "b0.changed"
238  [1]=>
239  &int(1)
240  [2]=>
241  array(2) {
242    [0]=>
243    &string(10) "b0.changed"
244    [1]=>
245    &int(1)
246  }
247}
248array(3) {
249  [0]=>
250  &string(10) "b0.changed"
251  [1]=>
252  &string(10) "b1.changed"
253  [2]=>
254  array(2) {
255    [0]=>
256    &string(10) "b0.changed"
257    [1]=>
258    &string(10) "b1.changed"
259  }
260}
261array(3) {
262  [0]=>
263  &string(12) "b2.0.changed"
264  [1]=>
265  &string(10) "b1.changed"
266  [2]=>
267  array(2) {
268    [0]=>
269    &string(12) "b2.0.changed"
270    [1]=>
271    &string(10) "b1.changed"
272  }
273}
274array(3) {
275  [0]=>
276  &string(12) "b2.0.changed"
277  [1]=>
278  &string(12) "b2.1.changed"
279  [2]=>
280  array(2) {
281    [0]=>
282    &string(12) "b2.0.changed"
283    [1]=>
284    &string(12) "b2.1.changed"
285  }
286}
287
288
289--- Containing array references 1 element in nested array:
290array(3) {
291  [0]=>
292  int(1)
293  [1]=>
294  &int(1)
295  [2]=>
296  &array(2) {
297    [0]=>
298    &int(1)
299    [1]=>
300    int(1)
301  }
302}
303string(48) "a:3:{i:0;i:1;i:1;i:1;i:2;a:2:{i:0;R:3;i:1;i:1;}}"
304array(3) {
305  [0]=>
306  string(10) "b0.changed"
307  [1]=>
308  &int(1)
309  [2]=>
310  array(2) {
311    [0]=>
312    &int(1)
313    [1]=>
314    int(1)
315  }
316}
317array(3) {
318  [0]=>
319  string(10) "b0.changed"
320  [1]=>
321  &string(10) "b1.changed"
322  [2]=>
323  array(2) {
324    [0]=>
325    &string(10) "b1.changed"
326    [1]=>
327    int(1)
328  }
329}
330array(3) {
331  [0]=>
332  string(10) "b0.changed"
333  [1]=>
334  &string(12) "b2.0.changed"
335  [2]=>
336  array(2) {
337    [0]=>
338    &string(12) "b2.0.changed"
339    [1]=>
340    int(1)
341  }
342}
343array(3) {
344  [0]=>
345  string(10) "b0.changed"
346  [1]=>
347  &string(12) "b2.0.changed"
348  [2]=>
349  array(2) {
350    [0]=>
351    &string(12) "b2.0.changed"
352    [1]=>
353    string(12) "b2.1.changed"
354  }
355}
356
357
358--- Containing array references 2 elements in nested array:
359array(3) {
360  [0]=>
361  &int(1)
362  [1]=>
363  &int(1)
364  [2]=>
365  &array(2) {
366    [0]=>
367    &int(1)
368    [1]=>
369    &int(1)
370  }
371}
372string(48) "a:3:{i:0;i:1;i:1;i:1;i:2;a:2:{i:0;R:2;i:1;R:3;}}"
373array(3) {
374  [0]=>
375  &string(10) "b0.changed"
376  [1]=>
377  &int(1)
378  [2]=>
379  array(2) {
380    [0]=>
381    &string(10) "b0.changed"
382    [1]=>
383    &int(1)
384  }
385}
386array(3) {
387  [0]=>
388  &string(10) "b0.changed"
389  [1]=>
390  &string(10) "b1.changed"
391  [2]=>
392  array(2) {
393    [0]=>
394    &string(10) "b0.changed"
395    [1]=>
396    &string(10) "b1.changed"
397  }
398}
399array(3) {
400  [0]=>
401  &string(12) "b2.0.changed"
402  [1]=>
403  &string(10) "b1.changed"
404  [2]=>
405  array(2) {
406    [0]=>
407    &string(12) "b2.0.changed"
408    [1]=>
409    &string(10) "b1.changed"
410  }
411}
412array(3) {
413  [0]=>
414  &string(12) "b2.0.changed"
415  [1]=>
416  &string(12) "b2.1.changed"
417  [2]=>
418  array(2) {
419    [0]=>
420    &string(12) "b2.0.changed"
421    [1]=>
422    &string(12) "b2.1.changed"
423  }
424}
425
426
427--- Nested array references container:
428array(3) {
429  [0]=>
430  int(1)
431  [1]=>
432  int(1)
433  [2]=>
434  &array(2) {
435    [0]=>
436    int(1)
437    [1]=>
438    &array(3) {
439      [0]=>
440      int(1)
441      [1]=>
442      int(1)
443      [2]=>
444      *RECURSION*
445    }
446  }
447}
448string(74) "a:3:{i:0;i:1;i:1;i:1;i:2;a:2:{i:0;i:1;i:1;a:3:{i:0;i:1;i:1;i:1;i:2;R:4;}}}"
449array(3) {
450  [0]=>
451  string(10) "b0.changed"
452  [1]=>
453  int(1)
454  [2]=>
455  &array(2) {
456    [0]=>
457    int(1)
458    [1]=>
459    array(3) {
460      [0]=>
461      int(1)
462      [1]=>
463      int(1)
464      [2]=>
465      *RECURSION*
466    }
467  }
468}
469array(3) {
470  [0]=>
471  string(10) "b0.changed"
472  [1]=>
473  string(10) "b1.changed"
474  [2]=>
475  &array(2) {
476    [0]=>
477    int(1)
478    [1]=>
479    array(3) {
480      [0]=>
481      int(1)
482      [1]=>
483      int(1)
484      [2]=>
485      *RECURSION*
486    }
487  }
488}
489array(3) {
490  [0]=>
491  string(10) "b0.changed"
492  [1]=>
493  string(10) "b1.changed"
494  [2]=>
495  &array(2) {
496    [0]=>
497    string(12) "b2.0.changed"
498    [1]=>
499    array(3) {
500      [0]=>
501      int(1)
502      [1]=>
503      int(1)
504      [2]=>
505      *RECURSION*
506    }
507  }
508}
509array(3) {
510  [0]=>
511  string(10) "b0.changed"
512  [1]=>
513  string(10) "b1.changed"
514  [2]=>
515  &array(2) {
516    [0]=>
517    string(12) "b2.0.changed"
518    [1]=>
519    string(12) "b2.1.changed"
520  }
521}
522