Skip to content

Commit a2f955b

Browse files
committed
docs: #140 array functions with nested lookup
1 parent db634f5 commit a2f955b

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

content/documentation/php-interop.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,35 @@ Equivalent to PHP's `arr[index] ?? null`.
112112
(php/aget (php/array "a" "b" "c") 5) # Evaluates to nil
113113
```
114114

115+
## Get nested PHP-Array value
116+
117+
```phel
118+
(php/aget-in arr path)
119+
```
120+
121+
Resolves nested values in a PHP array using a sequence of keys or indexes. The
122+
`path` must be a sequential collection such as a vector. If any step in the
123+
path is missing, `nil` is returned.
124+
125+
```phel
126+
(def users
127+
(php/array
128+
"users"
129+
(php/array
130+
(php/array "name" "Alice")
131+
(php/array "name" "Bob"))))
132+
133+
(php/aget-in users ["users" 1 "name"]) # Evaluates to "Bob"
134+
135+
(php/aget-in
136+
(php/array "meta" (php/array "status" "ok"))
137+
["meta" "status"]) # Evaluates to "ok"
138+
139+
(php/aget-in
140+
(php/array "meta" (php/array "status" "ok"))
141+
["meta" "missing"]) # Evaluates to nil
142+
```
143+
115144
## Set PHP-Array value
116145

117146
```phel
@@ -120,6 +149,22 @@ Equivalent to PHP's `arr[index] ?? null`.
120149

121150
Equivalent to PHP's `arr[index] = value`.
122151

152+
## Set nested PHP-Array value
153+
154+
```phel
155+
(php/aset-in arr path value)
156+
```
157+
158+
Creates or updates nested entries inside a PHP array. Intermediate arrays are
159+
created as needed to ensure the path exists before writing the value.
160+
161+
```phel
162+
(def data (php/array))
163+
(php/aset-in data ["user" "profile" "name"] "Charlie")
164+
(php/aget-in data ["user" "profile" "name"]) # Evaluates to "Charlie"
165+
# Equivalent to $data['user']['profile']['name'] = 'Charlie';
166+
```
167+
123168
## Append PHP-Array value
124169

125170
```phel
@@ -136,6 +181,22 @@ Equivalent to PHP's `arr[] = value`.
136181

137182
Equivalent to PHP's `unset(arr[index])`.
138183

184+
## Unset nested PHP-Array value
185+
186+
```phel
187+
(php/aunset-in arr path)
188+
```
189+
190+
Removes a nested entry in a PHP array. Once the value is removed, parent arrays
191+
remain untouched even if they become empty.
192+
193+
```phel
194+
(def data (php/array "user" (php/array "profile" (php/array "name" "Dora"))))
195+
(php/aunset-in data ["user" "profile" "name"])
196+
(php/aget-in data ["user" "profile" "name"]) # Evaluates to nil
197+
# Equivalent to unset($data['user']['profile']['name']);
198+
```
199+
139200
## `__DIR__`, `__FILE__`, and `*file*`
140201

141202
In Phel you can also use PHP Magic Methods `__DIR__` and `__FILE__`. When the

0 commit comments

Comments
 (0)