Skip to content
Advertisement

PHP Loop inside array

How can I loop inside an array in my code?

This is the static version of my script:

JavaScript

Then, I need to retrieve the value and label from the database.

So I customized my previous code to:

JavaScript

When I run it I get this Error message:

Parse error: syntax error, unexpected ‘while’ (T_WHILE), expecting ‘)’

Can anyone help me?

I want to loop the join data from my database into :

input type=”Select”

Advertisement

Answer

You can’t use a loop directly as a value in an array. Instead, loop over and add each array for each iteration of the while-loop.

Using $div[] = "value", you add that value as a new element in that array.

JavaScript

This will create a two-dimensjonal array which would look (as an example) like

JavaScript

…which you then can look through with a foreach to do what you want with it.


If you want this to be output as options in a select-element, you could just do that directly

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement