1

How can I show only name and lastname for mecanicien?

$aMecaniciens = array(
    array(
        "idMecanicien"=>1,
        'vchNomMecanicien'=>"Guérand",
        'vchPrenomMecanicien'=>"Bob"

        ),
    array(
        "idMecanicien"=>2,
        'vchNomMecanicien'=>"Lim",
        'vchPrenomMecanicien'=>"Bao"

        ),
    array(
        "idMecanicien"=>3,
        'vchNomMecanicien'=>"Cadoret",
        'vchPrenomMecanicien'=>"Cadoret"

        )
    );

foreach ($aMecaniciens as $value) {

    foreach ($value as $key) {
        echo "<option value=\"value\">$key[1].$key[2]</option>";
    }
}
2
  • 1
    You're using $key[1], but the arrays are not with a numbered index. Try using $key['vchNomMecanicien'] instead. Commented Jul 7, 2016 at 22:09
  • @Qirel $key does not hold the inner array, it holds things like Guérand and 3. Commented Jul 7, 2016 at 22:17

2 Answers 2

2

You don't need the inner foreach. You can use the named keys of $value to output your options.

foreach ($aMecaniciens as $value) {
    echo "<option value=\"$value[idMecanicien]\">
              $value[vchNomMecanicien].$value[vchPrenomMecanicien]
          </option>";
}

With the nested loop you will output three options for each item in $aMecaniciens, which I assume you don't want.

Sign up to request clarification or add additional context in comments.

Comments

0
foreach($aMecaniciens as $key => $value){
 echo '<option value="'.$value['idMecanicien'].'">'.$value['vchNomMecanicien'].' '.$value['vchPrenomMecanicien'].'</option>';
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.