-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace.php
More file actions
30 lines (24 loc) · 997 Bytes
/
Copy pathreplace.php
File metadata and controls
30 lines (24 loc) · 997 Bytes
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
<?php
// Replace a specific object in the JSONL file
function replaceObject($filename, $searchKey, $searchValue, $replacementObject) {
// Read the file
$file = file($filename);
// Iterate through each line and find the target object
foreach ($file as $index => $line) {
$object = json_decode($line);
// Check if the target object is found
if ($object->$searchKey == $searchValue) {
// Replace the object with the replacement object
$file[$index] = json_encode($replacementObject) . "\n";
break;
}
}
// Write the modified objects back into the file
file_put_contents($filename, implode("", $file));
}
// USAGE:
// Replace a specific object in the JSONL file
$searchKey = "name";
$searchValue = "John Doe";
$replacementObject = (object) array("name" => "John Smith", "age" => 35, "occupation" => "Manager");
replaceObjectInJSONL("data.ndof", $searchKey, $searchValue, $replacementObject);