Balakumar B
Balakumar B

Reputation: 790

How to skip Key and value from array inside foreach?

I have struggle in set start point in PHP array

PHP CODE

for($k=0; $k<count($AddClmn); $k++){
            $ord = 0;
            foreach($AddClmn[$k] as $ky=>$vl){
                $clmns[] = array('head'=>$ky, 'src'=>$vl, 'typ'=>'datatyp', 'NEMERIC'=>'', 'wdth'=>'70', 'ord'=>$ord);
                $ord++;
            }
        }
file_put_contents('Tracing.txt', print_r($clmns, true));

My actual output is above PHP code

Array
(
[0] => Array
    (
        [head] => locid
        [src] => 1
        [typ] => datatyp
        [NEMERIC] => 
        [wdth] => 70
        [ord] => 0
    )

[1] => Array
    (
        [head] => hhs
        [src] => 2525252
        [typ] => datatyp
        [NEMERIC] => 
        [wdth] => 70
        [ord] => 1
    )

[2] => Array
    (
        [head] => LA0
        [src] => 9831808.388559164
        [typ] => datatyp
        [NEMERIC] => 
        [wdth] => 70
        [ord] => 2
    )
)

in above result i want skip first two array and i want 3 rd array as start with index 0. how to set pointer or any way to face this situation? i except result is

[0] => Array
    (
        [head] => LA0
        [src] => 9831808.388559164
        [typ] => datatyp
        [NEMERIC] => 
        [wdth] => 70
        [ord] => 2
    )

[1] => Array
    (
        [head] => LA1
        [src] => 12920638.804462105
        [typ] => datatyp
        [NEMERIC] => 
        [wdth] => 70
        [ord] => 3
    )

how to solve this prob ?

Upvotes: 1

Views: 769

Answers (5)

Adrenaxus
Adrenaxus

Reputation: 1613

With PHP >= 5.3 Use this function to flatten (aka remove one "layer") your array by one level:

function flatten(array $array) {
    $return = array();
    array_walk_recursive($array, function($a) use (&$return) { $return[] =     $a; });
    return $return;
}

Upvotes: 0

Dharmendra
Dharmendra

Reputation: 127

// The following lines will remove values from the first two indexes.

    unset($array[0]);
    unset($array[1]);

// This line will re-set the indexes (new array will set from '0' index)

$array = array_values($array);

// The following line will show the new content of the array

 print_r($array);

Upvotes: 1

jitendrapurohit
jitendrapurohit

Reputation: 9675

I think you need this -

foreach($AddClmn[$k] as $ky=>$vl){
  if(substr( $ky, 0, 2 ) === "LA") {
    //your code
  }
} // end for loop

Upvotes: 1

Siddhartha esunuri
Siddhartha esunuri

Reputation: 1144

//you have an array like that

$data = array(
    '0' => 'Hello',   //you want to skip this

    '1' => 'Hello1', //you want to skip this

    '2' => 'Hello2',
    );   

 $skipped = array('0', '1');

    foreach($data as $key => $value){
        if(in_array($key, $skipped)){
            continue;
        }
        //do your stuf
    }

Upvotes: 1

Hardik
Hardik

Reputation: 439

for($k=2; $k<count($AddClmn); $k++){
            $ord = 0;
            foreach($AddClmn[$k] as $ky=>$vl){
                $clmns[] = array('head'=>$ky, 'src'=>$vl, 'typ'=>'datatyp', 'NEMERIC'=>'', 'wdth'=>'70', 'ord'=>$ord);
                $ord++;
            }
        }
file_put_contents('Tracing.txt', print_r($clmns, true));

Use this code..

Upvotes: 1

Related Questions