简体   繁体   中英

How can i execute foreach loop on multidimensional loop in php?

I have an array, which is consist of many arrays. So i want to execute foreach loop on it. I want to generate options from it.

Array

Array
(
    [0] => Array
        (
            [user_id] => 201
            [first_name] => ayaz
        )
    [1] => Array
        (
            [user_id] => 212
            [first_name] => khalique
        )
)

I have tried

 <?php foreach($UserDataEmail as $user){
            foreach($user as $val){?>
                <option value="<?=$val['user_id']?>" ><?=$val['first_name']?></option>
            <?php }
    }// end foreach ?>

But it does not work, can any one guide me where i'm wrong. Thanks

You do not need the next foreach loop as you can access the values directly from there -

 <?php foreach($UserDataEmail as $user){ ?>
     <option value="<?=$user['user_id']?>" ><?=$user['first_name']?></option>
 <?php }// end foreach ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM