当我没有';I don’我不知道钥匙


How to access an element in a nested array, when I don't know the key?

我有一个在WordPress上运行的表单插件,它可以上传下面一些字段的值:

[31]=>鹈鹕[29]=>阵列([FErQa]=>阵列([user_file_name]=>Pelican.jpg[文件名]=>pelican-006.jpg[file_path]=>/nas/content/live/mysite/wp-content/uploads/sites/2/nija-forms/[file_url]=>http://mysite/wp-content/uploads/sites/。。。an-006.jpg[完成]=>1[upload_id]=>19))

访问31 => Pelican没有问题,因为我知道密钥。问题出在29FErQa中的file_path,因为FErQa密钥每次上传都会更改,而我不知道密钥。

那么,当我不知道密钥FErQa时,如何在FErQa中访问file_path

如果$arr[29]:中只有一个元素

$item = reset($arr[29]);
if ($item) {
  $search_key = key($arr[29]);
  // Work with $search_key and $item
}

如果可能有几个项目,请查找具有定义file_path:的项目

$found = false;
reset($arr[29]);
while (!$found && (list($search_key, $item) = each($arr[29]))) {
  if (isset($item['file_path'])) {
    $found = true;
  }
}
if ($found) {
  // Work with $search_key and $item
}

在这两种情况下,发现根本不需要对某个变量进行键分配(显示为明确),您可以简单地使用$item进行操作。

相关文章: