-
Notifications
You must be signed in to change notification settings - Fork 8k
Expand file tree
/
Copy patharray_get.phpt
More file actions
77 lines (64 loc) · 1.87 KB
/
array_get.phpt
File metadata and controls
77 lines (64 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
--TEST--
Test array_get() function
--FILE--
<?php
/*
* Test functionality of array_get()
*/
echo "*** Testing array_get() ***\n";
// Basic array access
$array = ['products' => ['desk' => ['price' => 100]]];
// Test nested access with dot notation
var_dump(array_get($array, 'products.desk.price'));
// Test with default value when key doesn't exist
var_dump(array_get($array, 'products.desk.discount', 0));
// Test simple key access
$simple = ['name' => 'John', 'age' => 30];
var_dump(array_get($simple, 'name'));
var_dump(array_get($simple, 'missing', 'default'));
// Test with integer key
$indexed = ['a', 'b', 'c'];
var_dump(array_get($indexed, 0));
var_dump(array_get($indexed, 5, 'not found'));
// Test with null key (returns whole array)
$test = ['foo' => 'bar'];
var_dump(array_get($test, null));
// Test nested with missing intermediate key
var_dump(array_get($array, 'products.chair.price', 50));
// Test single level key that doesn't exist
var_dump(array_get($array, 'missing'));
// Test with numeric string in path (like users.0.name)
$users = ['users' => [['name' => 'Alice'], ['name' => 'Bob']]];
var_dump(array_get($users, 'users.0.name'));
var_dump(array_get($users, 'users.1.age', 70));
// Test with array key (equivalent to dot notation)
var_dump(array_get($array, ['products', 'desk', 'price']));
var_dump(array_get($simple, ['name']));
var_dump(array_get($users, ['users', 0, 'name']));
var_dump(array_get($array, ['products', 'chair', 'price'], 75));
// Test with invalid segment type in array key
var_dump(array_get($array, ['products', new stdClass(), 'price'], 'invalid'));
echo "Done";
?>
--EXPECT--
*** Testing array_get() ***
int(100)
int(0)
string(4) "John"
string(7) "default"
string(1) "a"
string(9) "not found"
array(1) {
["foo"]=>
string(3) "bar"
}
int(50)
NULL
string(5) "Alice"
int(70)
int(100)
string(4) "John"
string(5) "Alice"
int(75)
string(7) "invalid"
Done