Best method for storing arrays in database

Following on from the excellent helpful thread here:

I am now working on the Server Connect API script.

My form is sending four arrays:
response[0]: na
scalevalue[0]: 5
questionid[0]: 1
comments[0]:
response[1]: answered
scalevalue[1]: 5
questionid[1]: 2
comments[1]:
etc.

I can set up a repeat for them individually but I want to store the four variables in a record for each one. So I want to process response[0], scalevalue[0], questionid[0] and comments[0] before moving onto all the [1] values, etc.

What's the technique for this?

Assuming there is ALWAYS a value for each and thus an equal length for each array, you can perform a repeat on one of the arrays, and then reference the other arrays value with $index.

For example:

And then in the insert:

ingredient get $value because it is the array being repeated. the other POST arrays are referenced using $index.

But again, this relies on each array being full, and being aligned by index.

2 Likes

That looks to be exactly what I need. Yes, the arrays will be aligned so that should work fine. Will give it a go. Big thanks Ken.