RE: [PHP] Going loopy with arrays.....
| From: | Jay Blanchard | Date: | Fri, 31 Mar 2006 18:02:46 +0000 |
| Subject: | RE: [PHP] Going loopy with arrays..... | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-233003@lists.php.net to get a copy of this message | ||
[snip]
> a. loop through it and recognize when I have com upon a new sub-array so
> that I can do 'new' output
> 2. OR get each of the sub-arrays out as individual arrays.
This might help get you going in the right direction...
<script language="php">
function print_elements( $var ) {
if( is_array( $var )) {
foreach( $var as $k => $v ) {
if( is_array( $v )) {
print_elements( $v );
} else {
echo "$k => $v<br>";
}
}
}
}
$array = array();
$array['H7'][] = array( 'lon' => -99.2588, 'lat' => 29.1918
);
$array['H7'][] = array( 'lon' => -99.2205, 'lat' => 29.1487
);
$array['H7'][] = array( 'lon' => -99.242, 'lat' => 29.1575 );
$array['H7'][] = array( 'lon' => -99.2588, 'lat' => 29.1545
);
$array['H6'][] = array( 'lon' => -99.0876, 'lat' => 29.216 );
$array['H6'][] = array( 'lon' => -99.0618, 'lat' => 29.179 );
array_walk( $array, 'print_elements' );
</script>
[snip]
Okay, that is cool too, but it still lacks one key ingredient, I need to know which 'H'
each lat an lon belongs to.
Here is the pseudo-code for what I am attempting, I fear I may not have explained myself clearly
enough earlier;
Loop
Get the current H
While current H
output the points of current H
end while
next H
End loop
It is gotten to the point where I am tempted to put each H in its own file, open it, do the stuff,
close it, go to the next file as it might have been quicker but I so wanted to do this with the
array. The complication is that when the file is sent the H's are not in order, so they have to
be grouped together (with changing their order in the file) so that the output occurs logically.
Thanks everyone!