itkhansunny
10/12/2018 - 3:11 AM

Skipped some index php foreach loop

Skipped some index php foreach loop

 <?php
 $data = array(
            'Hello world',
            'Hello world2',
            'Hello world3',
            'Hello world4',
            'Hello world5',// you want to skip this
            'Hello world6',// you want to skip this
            'Hello world7',
            'Hello world8',
            'Hello world9',
            'Hello world10',//you want to skip this
        );
        
    $skipped=array(4,5,9);
    foreach ($data as $key => $field){
        if(in_array($key,$skipped)) continue;
        echo $field.'<br>';
    }
}
  <?php 
  //you have an array like that
        $data = array(
            '1' => 'Hello world',
            '2' => 'Hello world2',
            '3' => 'Hello world3',
            '4' => 'Hello world4',
            '5' => 'Hello world5',// you want to skip this
            '6' => 'Hello world6',// you want to skip this
            '7' => 'Hello world7',
            '8' => 'Hello world8',
            '9' => 'Hello world9',
            '10' => 'Hello world10',//you want to skip this
        );

        //Ok Now wi make an array which contain the index wich have to skipped

        $skipped = array('5', '6', '10');

        foreach($data as $key => $value){
            if(in_array($key, $skipped)){
                continue;
            }
            echo $value.'<br>';
        }