Jump to content

Looping An Array.


Crew-Portal

Recommended Posts

How do I take the array

<?php $xml['FSHOST']['0']['FLIGHTPLANS']['0']['FLIGHTPLAN']['0']['PLAYERNAME']['0']['VALUE'] ?>

and loop it until it finds a user named $_POST['username']. The way the array works is like

<?php
$xml['FSHOST']['0']['FLIGHTPLANS']['0']['FLIGHTPLAN']['0']['PLAYERNAME']['0']['VALUE']
$xml['FSHOST']['0']['FLIGHTPLANS']['0']['FLIGHTPLAN']['1']['PLAYERNAME']['0']['VALUE']
$xml['FSHOST']['0']['FLIGHTPLANS']['0']['FLIGHTPLAN']['2']['PLAYERNAME']['0']['VALUE']
// and ect ect using the is_array function so it doesnt loop forever
?>

Link to comment
https://forums.phpfreaks.com/topic/81118-looping-an-array/
Share on other sites

I would like the array to loop itself while is_array() until it finds an array with a value of whatever the user entered whcih can be carried over by a $_POST['username'] Global. I think it would be done like

<?php
$array = '0';
while (is_array($xml['FSHOST']['0']['FLIGHTPLANS']['0']['FLIGHTPLAN'][$array]['PLAYERNAME']['0']['VALUE'])){
if ($xml['FSHOST']['0']['FLIGHTPLANS']['0']['FLIGHTPLAN'][$array]['PLAYERNAME']['0']['VALUE'] = $_POST['username']){
echo 'Ya!';
else $array++;
}
}
?>

 

Thats something I just slapped together. It probably wont work though. My real concern is I do not know how to use While loops and I do not know how to stop a loop. Did I do it right?

Link to comment
https://forums.phpfreaks.com/topic/81118-looping-an-array/#findComment-411631
Share on other sites

foreach($array as $val){
if(is_array($val)){
	foreach($value as $x ){
		// do some stuff here
		$cnt++;
		if(count($value == $cnt)){
			if (is_array($x)){
			$value = $x;
                                $cnt = 0;
			}
		}
	}
}
}

not tested but i think this kind of sysntax will work not i guess that is only applicable for single dimensional array

Link to comment
https://forums.phpfreaks.com/topic/81118-looping-an-array/#findComment-411638
Share on other sites

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.