1

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 ?

1
  • Array ( [0] => Array ( [locid] => 1 [hhs] => 2525252 [LA0] => 9831808.388559164 [LA1] => 12920638.804462105 [LA2] => 6345274.235857028 [LA3] => 2600257.9218634316 [LA4] => 1136632.8957946603 [LA5] => 537362.7534893887 [LA6] => 771599.00 [LA7] => 941243.00 [LA8] => 370525.00 [LA9] => 217441.00 [LA10] => 59977.00 [LA11] => 19811.00 )) This is actually my $AddClmn array Commented Jul 21, 2016 at 6:45

5 Answers 5

1

I think you need this -

foreach($AddClmn[$k] as $ky=>$vl){
  if(substr( $ky, 0, 2 ) === "LA") {
    //your code
  }
} // end for loop
Sign up to request clarification or add additional context in comments.

Comments

1
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..

Comments

1

//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
    }

3 Comments

in your example set numeric index but in my array contain string as index like 'locid'=>15565
As like as same $skipped = array('locid');
if you have array in array you can use this as $skipped = array(array('0', '1'));
1

// 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);

1 Comment

I have used unset() but 3rd index is 2 but i want 3rd index as 0 after unset();
0

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;
}

Comments

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.