【问题标题】:PHP: Find index of array in multidimentional array based on given valuePHP:根据给定值查找多维数组中的数组索引
【发布时间】:2014-09-27 14:31:32
【问题描述】:

我正在尝试设置一个表单以允许用户更新大型 JSON 文件中的数据。我有一个表单,允许用户获取他们想要的“事件”并对其进行编辑。当他们单击提交时,它会将数据发送到一个脚本,该脚本将整个 JSON 文件拉入一个数组。从那里,我需要脚本来搜索该数组并找到与已编辑的“事件”具有相同 ID 的子数组的索引。因此,如果用户编辑了名为“William Bradford Born”的条目,我需要脚本匹配 ID“american-ccincore-1411541230”并为该子数组的索引返回 0。

[
  {
     "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"
       }
     ]
  }
]

我已尝试调整这里十几篇文章中建议的脚本,但在这种特殊情况下我无法使用任何脚本。感谢您的任何建议。

【问题讨论】:

  • 将其更改为 var_export() 而不是 var_dump(),这样人们就不必重新输入数据结构
  • 您正在处理的代码在哪里?
  • 现在数据处于可用状态,下一个明显的问题是“在编辑事件中来回发送什么数据?”例如,如果你只得到修改后的值,你将如何找到原始值是什么?
  • 你能在数组的前后放一个吗?以及您将接受更改数组的输入类型(基本上是 TML 要求的)?需要更多信息才能使用。另外,当您使用它时,它是 PHP 数组还是 json 对象?

标签: php json multidimensional-array arrays


【解决方案1】:

这只是一个简单的循环。需要注意的是,您需要区分0 的答案和未能找到您要查找的事件。我通常使用false 表示失败,但这意味着稍后使用===!== 进行比较。

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 ...

【讨论】:

  • 你怎么能像 $oneEvent->id 一样添加 ..$oneEvent 不是任何类的对象??
  • 我假设 JSON 数据是用json_decode() 解码的。你得到的是一个具有名为events 的属性的对象,它是一个对象数组。这些对象有一个id 属性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-24
  • 1970-01-01
相关资源
最近更新 更多