-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharray_path_test.php
More file actions
74 lines (64 loc) · 1.97 KB
/
Copy patharray_path_test.php
File metadata and controls
74 lines (64 loc) · 1.97 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
<?php
include 'array_path.php';
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_QUIET_EVAL, 1);
// Create a handler function
function my_assert_handler($file, $line, $code)
{
echo "<hr>Assertion Failed:
File '$file'<br />
Line '$line'<br />
Code '$code'<br /><hr />";
}
// Set up the callback
assert_options(ASSERT_CALLBACK, 'my_assert_handler');
$player = array(
'level' => 10,
'silver' => 20,
'building' => array(
'blacksmith' => array(
'level' => 2,
'store' => array(
'sword' => 2,
),
),
),
'heroes' => array(
array(
'id' => 1,
'name' => 'jack',
'attr' => array(
'str' => 10,
'def' => 20,
),
),
array(
'id' => 2,
'name' => 'jane',
'attr' => array(
'str' => 13,
'def' => 26,
),
),
),
);
assert('array_path_get($player, "level") === 10'); // return 10
assert('array_path_get($player, "building/blacksmith/level") === 2'); // return 2
//combine with any params
assert('array_path_get($player, "building", "blacksmith/level") === 2'); // return 2
assert('array_path_get($player, "building/blacksmith", "level") === 2'); // return 2
assert('array_path_get($player, "building", "blacksmith", "level") === 2'); // return 2
assert('array_path_get($player, "heroes", 0, "attr") === $player["heroes"][0]["attr"]'); // return array('str' => 10, 'def' => 20)
assert('array_path_get($player, "heroes/0/attr") === $player["heroes"][0]["attr"]'); // return array('str' => 10, 'def' => 20)
array_path_set($player, 'heroes/0/attr/str', 15);
assert('$player["heroes"][0]["attr"]["str"] === 15');
array_path_set($player, 'heroes', 2, array('id' => 3));
assert('$player["heroes"][2]["id"] === 3');
array_path_unset($player, 'building/blacksmith');
assert('!isset($player["building"]["blacksmith"])');
array_path_unset($player, 'heroes', 0, 'attr');
assert('!isset($player["heroes"][0]["attr"])');
array_path_walk($player, function($key, $value) {
echo $key . " : " . $value . "\n";
});