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