0

I'm using webforms to get some data from users. These results are later used in another php script, and this relies on looping through the data:

for ($i = 1; $i <= count($data); $i++)
    {
        $answers[] = $data[$i]["value"][0];        
    }  

However, by adding and deleting questions in the webform, the indices look something like this now:

1
2
3
5
8
12

And this gives an error because I use count() to get the number of entries. (count() = 6, but max() = 12). My first idea is to find a way to automatically reset the indices after adding or removing questions. So my indices look like this:

1
2
3
4
5
6

But I honestly have no idea how to go about this. Any ideas?

3 Answers 3

1

Well, as I got it, the question is not related to Webforms (which I'm not familiar with) but to PHP itself.

Besides "for each" option there is array_values() function. (If you don't want to change your script that processes the array).

$arr = array_values($arr);

link and link

1
  • This should do the trick, thank you! What I really wanted to do though, was reset the indices of the database itself. They are still not properly ordered there. Commented Aug 20, 2012 at 8:52
1

I might be misunderstanding you but I think you just want a foreach loop:

foreach ($data as $item) {
  $answers[] = $item["value"][0];        
}  

Or even shorter:

$answers = array_map(function($item) { return $item['value'][0]; }, $data);
1
  • Well, that I didn't think of that. I thought I really needed to use an index in my $item array. Commented Aug 20, 2012 at 8:58
0

You can use the webform_get_submissions($nid); loop the out put of this function using foreach. You will get the values without any issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.