0

I have a form plugin running on WordPress that uploads the values below for some of the fields:

[31] => Pelican
[29] => Array (
    [FErQa] => Array (
        [user_file_name] => Pelican.jpg 
        [file_name] => pelican-006.jpg
        [file_path] => /nas/content/live/mysite/wp-content/uploads/sites/2/ninja-forms/ 
        [file_url] => http://mysite/wp-content/uploads/sites/ ... an-006.jpg
        [complete] => 1
        [upload_id] => 19
    )
)

Accessing 31 => Pelican is no problem, since I know the key. The problem is with file_path in FErQa in 29, since the FErQa key changes every upload and I don't know the key.

So how can I access file_path in FErQa, when I don't know the key: FErQa?

Rizier123
  • 58,877
  • 16
  • 101
  • 156
Steve
  • 4,534
  • 9
  • 52
  • 110
  • So you don't know how to access the file path element? – Rizier123 Dec 13 '15 at 01:42
  • That's correct. Given that I know that array[29] holds the uploaded file info for, say img1 and the uploaded structure is as above, I need to write img1_file_path = ? – Steve Dec 13 '15 at 02:04
  • See the linked duplicate above, it will show you the solution how to access the elements. – Rizier123 Dec 13 '15 at 02:08
  • No, it doesn't. I know how to access elements in multiple arrays. The difference here is that the third array is at the FErQa element of the second array, and that offset is different every time a file is uploaded. I've tried the obvious array[29][FErQa][file_path] and it doesn't bring back the path. Even if it did it wouldn't solve my problem because I never know what the offset will be within the second array. I just know the whole thing is at the 29th element of the first array. – Steve Dec 13 '15 at 04:40
  • I reopened your question and edited it, so it is clear now what you want. To ask a good question please see [ask] and [The perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) – Rizier123 Dec 13 '15 at 10:39

1 Answers1

1

If there is one and only one element in the $arr[29]:

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

If there are possible several items, look for the one with defined 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
}

In both cases found key assignment to some variable is not required at all (showed to be clear), you can simply operate with $item.

Max Zuber
  • 1,217
  • 10
  • 16