I am trying to set up a form to allow users to update data in a large JSON file. I have a form that allows the user to grab the 'event' they want and to edit it. When they click submit, it sends the data to a script that pulls the entire JSON file into an array. From there, I need the script to search through that array and find the index of the sub-array that has the same ID as the 'event' that has been edited. So if the user has edited the entry called "William Bradford born", I need the script to match the ID "american-ccincore-1411541230" and return 0 for that sub-array's index.
[
{
"id" : "american",
"title" : "A Timeline of American Literature",
"description" : "LENGTHY DESCRIPTIVE TEXT",
"initial_zoom" : "50",
"focus_date" : "1650-01-01 00:00:00",
"size_importance" : "true",
"timezone" : "-06:00",
"min_zoom" : "20",
"max_zoom" : "80",
"image_lane_height" : "50",
"display_zoom_level" : "1",
"tags" : {
"Puritan" : "0",
"Enlightenment" : "0",
"Romantic" : "0",
"Transcendental" : "0",
"Dark Romantic": "0",
"African American": "0",
"American Indian": "0",
"International" : "0"
},
"legend": [
{
"title": "Author event",
"icon": "star_red.png"
},
{
"title": "Publication event",
"icon": "square_blue.png"
},
{
"title": "Historical event",
"icon": "triangle_green.png"
}
],
"events": [
{
"id": "american-ccincore-1411541230",
"title": "William Bradford born",
"description": "LENGTHY DESCRIPTIVE TEXT",
"tags": "Puritan",
"startdate": "1950-03- 00:00:00",
"enddate": "1657-05- 00:00:00",
"importance": "50",
"date_display": "year",
"link": "",
"image": "https://www.csustan.edu/sites/default/files/ENGLISH/reuben/pal/chap1/bradford.gif",
"icon": "star_red.png",
"span_color": "#f66"
}, {
"id": "american-mforkner-1411364607",
"title": "Church Mission Society",
"description": "LENGTHY DESCRIPTIVE TEXT",
"tags": "",
"startdate": "1799-01-01 00:00:00",
"enddate": "1799-01-01 00:00:00",
"importance": "50",
"date_display": "year",
"link": "",
"image": "http://webarchive.cms-uk.org/_images/tnsaies1.jpg",
"icon": ".png",
"span_color": "#ccc"
}
]
}
]
I've tried to adapt scripts suggested in a dozen or so posts here, but I can't get any to work in this particular situation. Thanks for any advice.
It's just a simple loop. The caveat is you need to distinguish between an answer of 0
and a failure to find the event you were looking for. I typically use false
to indicate failure, but that means using ===
or !==
for your comparison later.
function findEventIndexById($events, $target) {
$retval = false;
foreach($events as $index=>$oneEvent) {
if ($oneEvent->id == $target) {
$retval = $index;
break;
}
}
return $retval;
}
$foundIndex = findEventIndexById($data->events, $eventId);
if ($foundIndex !== false) {
// ... do stuff here to $data->events[$foundIndex]
} else
// ... report an error ...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.