0

A multidimensional returned from php, here is my php array:

$brands = array("Bmw" => "200" ,"Mercedes" => "201", "Audi" => "202");

When the data is returnend how can I populate a select with this info?

Lets assume that I need fill a select with Brands and their ID.

<select>
  <option value="200">Bmw</option>
  <option value="201">Mercedes</option>
  <option value="202">Audi</option>
</select>

I dont know how to loop through the returned array to fill the select.

Thanks a lot!!

2
  • I don't know what AJAX have to do here... Is your $brands sent to your ajax response in JSON, HTML or whatever format? Need to see the ajax call Commented Jun 13, 2014 at 16:40
  • I send the data back from the PHP with json_encode() Commented Jun 13, 2014 at 16:41

1 Answer 1

2

Use it this way.

$brands = array("Bmw" => "200" ,"Mercedes" => "201", "Audi" => "202");
$output = "<select>";
foreach($brands as $keys => $val){
    $output .= '<option value="'.$val.'">'.$keys.'</option>';
}
$output .= "</select>";
echo $output;
Sign up to request clarification or add additional context in comments.

1 Comment

I cant do that, i have to wait 5min :D

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.