Pikachu2000 Posted July 5, 2010 Share Posted July 5, 2010 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 https://forums.phpfreaks.com/topic/206739-array-woes/ Share on other sites More sharing options...
kenrbnsn Posted July 5, 2010 Share Posted July 5, 2010 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 https://forums.phpfreaks.com/topic/206739-array-woes/#findComment-1081173 Share on other sites More sharing options...
PFMaBiSmAd Posted July 5, 2010 Share Posted July 5, 2010 array_slice is probably the best bet to get the 8th - 15th elements of the array you want for further processing. Link to comment https://forums.phpfreaks.com/topic/206739-array-woes/#findComment-1081176 Share on other sites More sharing options...
Pikachu2000 Posted July 5, 2010 Author Share Posted July 5, 2010 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 https://forums.phpfreaks.com/topic/206739-array-woes/#findComment-1081177 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.