Jump to content

php noob - can't figure this out - Arrays


Nacman

Recommended Posts

I have a website that I have curled some data from and gotten the data I want. It is one large array.  The variable is called $ten_pireps.

 

For clarity I am only posting the first 8 keys=>value pairs. The array repeats the info every 8 keys. ending with key [79].

 

Array ( [0] => ET20029 [1] => EAL391 [2] => ESKN [3] => EETN [4] => E35 [5] => 0.8 [6] => 284 [7] => 01/02/2011

 

 

I need to iterate through this 79 key array and place keys [0] thru [7] into another variable array

called $flightinfo1, keys [8] thru [15] into $flightinfo2, etc.....

finish with the final array $flightinfo10.

 

Secondly, I am trying to name the keys of the new 10 smaller seperate arrays

 

with key names [Flight_Num] [Call_Sign] [Dep_Icao] [Arr_Icao] [Equip] [Flt_Time] [Flt_Distance] [Date]

being the same for each array.  Ultimately I will be inserting this into a db.

 

I suck with arrays, and have tried to modify code snippets and have had no success.

 

Here is something I was trying but just could echo anything out that made sense.

 

foreach ($ten_pireps as $key=>$value) {

}

 

for ($i=0,$j=1;$i<count($ten_pireps); $i+=8,$j++) {

$flight="flightinfo";

$flightsql="$flight$j"; // create new variable name array as it iterates

$flightsql[Flight_Num] = $ten_pireps[$i];

$flightsql[Call_Sign] = $ten_pireps[$i+1];

$flightsql[Dep_Icao] = $ten_pireps[$i+2];

$flightsql[Arr_Icao] = $ten_pireps[$i+3];

$flightsql[Equip] = $ten_pireps[$i+4];

$flightsql[Flt_Time] = $ten_pireps[$i+5];

$flightsql[Flt_Distance] = $ten_pireps[$i+6];

$flightsql[Date] = $ten_pireps[$i+7];

// unfinished cause it is wrong!!!!

}

 

Thanks for the read - Nacman

Link to comment
https://forums.phpfreaks.com/topic/223217-php-noob-cant-figure-this-out-arrays/
Share on other sites

Consider using array_chunk(), and it will sort them into a multi-dimensional array for you.  This will give you flights[0] - flights[9], re-indexing the keys so your values will reside in flights[0][0] - flights[0][7].

 

$flights = array_chunk($ten_pireps,;


echo '<pre>';
print_r($flights[0]);
print_r($flights[1]);
print_r($flights[2]);
print_r($flights[3]);
print_r($flights[4]);
print_r($flights[5]);
print_r($flights[6]);
print_r($flights[7]);
print_r($flights[8]);
print_r($flights[9]);
echo '</pre>';

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.