Jump to content

need help with this


devWhiz

Recommended Posts

How would I go about doing this...

 

I want to have a few variables equaling either true or false... if true, it puts 'x' number into array

I dont know how to add more than one 'x' number into the array like this

 


$include_a = true;
$include_b = false;
$include_c = true;

 

if include_a is true, add 24 to the array, include_b is false so dont add 5 to the array, include_c is true so add 13

to the array...

 


$include_a = true; // if true, add 24 to the array
$include_b = false; // if true, add 5 to the array
$include_c = true; // if true, add 13 to the array

if($include_a == true || $include_b == true || $include_c == true)
{
$array = array(24, 5, 13);
}


 

now since include_b is false, it should only add 24 and 13 to the array.. do I use else if? or many if statements or what?

 

this is probably going to confuse everyone, its kind of hard to explain

Link to comment
Share on other sites

Going strictly based on the description you've provided:

 

$array = array();
if( $include_a === TRUE ) {
     $array[] = 24;
}
if( include_b === TRUE ) {
     $array[] = 5;
}
if( include_c === TRUE ) {
     $array[] = 13;
}

 

If you explain the purpose behind all of this, a better solution may become apparent.

Link to comment
Share on other sites

that sounds like a lot of extra nonsense to me, no offence..

 

maybe giving your the variables the actual numbers you want in the array would be better.. then

 

$myNewArray = array();
if($include_a !== false){$myNewArray[]=24;}
if($include_b !== false){$myNewArray[]=5;}
if($include_c !== false){$myNewArray[]=13;}

echo "<pre>";
print_r();
echo "</pre>";

 

this way if the variable is anything other than false (true, in your case) it will add an item to the variable $myNewArray which at the begining is defined as an empty array.. doing $myNewArray[] appends to an array

Link to comment
Share on other sites

basically.. I am reading an xml file that is as follows

 

<outer>
<xml>
	<establishments>
		<land>
			<id>24</id>
			<cost>920000000000</cost>
			<income>$100,000,000</income>
                                <name>NAME</name>
		</land>
		<land>
			<id>17</id>
			<cost>921000</cost>
			<income>$100</income>
                                <name>NAME</name>
		</land>
		<land>
			<id>3</id>
			<cost>2763000</cost>
			<income>$300</income>
                                <name>NAME</name>
		</land>
	</establishments>
</xml>
</outer>

 

I use simplexml to parse the xml file and put the objects in an array, well.. you see how there is an ID assigned for each <land> well what I want to do is use

 


$include_a = true;
$include_b = false;
$include_c = true;

 

if true it puts certain ids in an array, and then I will use if(in_array()) to check to see if the id is in the array, if it is, it skips the calculation, basically the script is just reading the xml file, dividing the income by the cost to get a certain ratio, well the script wont block out any ids if I dont set it to true or false on whether or not to include the id in the array, I know i could probably use if statements to check and see if the variables are true or false, if true it updates the array with the id and then I use in_array to search, if the id is in the array it will continue;

 

I know I could do this to make it easier on myself

 


if(!in_array($ID, $IDarray))
    {
    calculate ratios
    } continue;

Link to comment
Share on other sites

im really exhausted, been working on moving to a new house, I will try to explain further in better detail if you guys dont get what I am saying, I can't think straight right now, yet I still insist of trying to add features to a script of mine, ;P

 

thank you guys for the responses as well, always appreciated

Link to comment
Share on other sites

Gotta agree with the other guys, what you're doing sounds a bit weird...

 

from what I understand, $include_a is always the first element retrieved from the xml, and $include_b is always the second, and $include_c is always the third?

 

And then you manually list the wanted/unwanted id's and create a variable for each, in which you assign true of false?

 

I don't see much point to this. Too much stuff to do manually.

 

What would happen if you xml file retuned 20 different values? would you manually create a variable d, e, f, g... etc... for each?

 

I would load the entire XML into the array, and have a second array with only the numbers I want to process.... Like you said, in_array() can then solve the rest...

 

so try something like this: (assuming you have an array called $xmlFile with the loaded data)

 

$validIDS = array('24','5');

foreach($xmlFile as $k => $item){

    if(in_array($item['id'],$validIDS){

      //.. Calculate

    }else{

      // you can remove the unwanted ones from the array if necessary

      unset($item[$k]);

    }

}

 

this way, all you need to do is update the ids in $validIDS. it's still not a great way to do it, but I don't really understand what your selection logic is, how do you decide if you want ID number 24 or not?

 

hope this helps

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.