2

I have a simple foreach loop as follows:

foreach ($d_38 as $value) {
    echo "
        <option value='".$value."'";
            if ($results["q".$i]==$value) echo 'selected="selected"';     
    echo">".$value."</option>
";
}

I currently have the the information stored in an array called $d_38 this information is placed into the options of the drop down during the foreach loop.

That all works fine. However, I have a different language stored in $d_38_t that I want to show when the text of the option is shown, in the code above this is the third $value variable. So basically, the user sees the options in one language but the data is always stored in English in this case.

I've no idea how to put those two arrays together so I can use them in the foreach loop - can anyone offer any guidance please?

6
  • 3
    I feel like this is missing php, no? -- And, FWIW, this isn't a jQuery question at all. EDIT Much better. Commented Jun 5, 2015 at 19:43
  • I'm suitably embarrassed...been entrenched with jQuery today...hence it's on my brain...thanks Brad! Commented Jun 5, 2015 at 19:44
  • Probably much easier to use a regular for loop to iterate over one array and inside access data from both arrays, provided they are sorted the same way. Commented Jun 5, 2015 at 19:45
  • $d_38 and $d_38_t has the same keys? Commented Jun 5, 2015 at 19:46
  • Yep, same keys panther. Commented Jun 5, 2015 at 19:50

2 Answers 2

3

Add key to foreach and use it in as the key of $d_38_t.

foreach ($d_38 as $key => $value) {
    echo "<option value='" . $value . "'";
        if ($results["q".$i] == $value) echo 'selected="selected"';    
    echo ">" . $d_38_t[$key] . "</option>";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Probably better to stick with foreach. Keys may not be numeric, and foreach will retain this relationship.
3

Assuming the keys are numeric and align between $d_38 and $d_38_t, you can use a for loop:

for ($i = 0; $i < count($d_38); $i++)
{
    // ...snip...
    echo ">" . $d_38_t[$i] . "</option>";
}

Going to keep it, but do be warned: this only works with numeric keys. @panther has the correct answer. ;-)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.