Jump to content

Array woes.


Pikachu2000

Recommended Posts

Alright, so I'm not the best with arrays, but I've never had to try this either. I have a text file I've parsed through into array elements. Each element has from 8-15 subelements. I'm fine to this point. where I'm running into a wall, is that indices 0-7 are fine the way they are, but indices 8 and up need to be implode()d to a single string, and end up in element 8 of the array. The rest can then be truncated, but that isn't a necessity, since it will be inserted into a DB, then the array will be destroyed.

 

 

There are 75-100,000 text file lines, so I need to do this in a loop. I've tried if/else conditionals on the key, with implode(), concatenation in to a string and reassignment to another element, and I just know I'm missing something stupidly simple. Here's the basic code I have, with the stuff that didn't work stripped out, along with an example of what the array looks like.

 

 

//This extracts data with type flag '2' from the text file

foreach( $nav_data as $k => $v ) {
$v = preg_replace('/\s+/', ' ', $v);
if( stripos($v, '2') === 0 ) {
	//$ndb[] = $v;
	$exp_ndb[] = explode(' ', trim(rtrim($v)) );
}


//This is what is giving me hell and not doing a damn thing, no matter what I do within it.

$i = 0;
foreach($exp_ndb as $val) {
foreach( $val as $k => $v ) {
// Tried other loops, unset() and assign, and conditionals here. All have been epic fail.
	$formatted_ndb[$i][] = $v;
}
$i++;
}

Array(    [0] => Array        (            [0] => 2            [1] => 38.08783340            [2] => -77.32499695            [3] => 0            [4] => 396            [5] => 50            [6] => 0.000            [7] => APH            [8] => A     [8]-up should end up in a different element together as a single-spaced string "A P HILL NDB"            [9] => P            [10] => HILL            [11] => NDB        )    [1] => Array        (            [0] => 2            [1] => 57.08382034            [2] => 9.68009281            [3] => 0            [4] => 398            [5] => 25            [6] => 0.000            [7] => GL            [8] => AALBORG            [9] => NDB        )

Link to comment
Share on other sites

So what you're saying is that you're final array needs no more than 9 elements in it numbered 0 through 8.  If that's the case just use the third parameter to the explode function to limit the initial array to the 9 elements:

<?php
$exp_ndb[] = explode(' ', trim(rtrim($v)),9 );
?>

 

Ken

Link to comment
Share on other sites

Oh, for Pete's sake. See, I knew it would be something like that. I was overlooking the simple in pursuit of the absurdly complex.

 

Thanks kenrbnsn, that does exactly what I need.

 

And PFM, I'll look into array_slice() so I at least I'll have it for future reference. Thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.