Jump to content

Recommended Posts

For some reason using a variable with the in_array() does not work even though the structure in the variable is correct.... heres pretty much what i've got but it doesn't work:

 

<?php
$Function = '"0000","0010","9000"';
$Array = array($Function);
If(in_array($cord,$Array)){echo $cord;}Else{
	echo '<input type="checkbox" name="list[]" value="'.$cord.'">';
}	
echo '</center>';
?>

 

Why won't it allow me to just put the variable function as part of the array?

Link to comment
https://forums.phpfreaks.com/topic/153486-solved-array-variable-help/
Share on other sites

and your $Function is not the array you think it is.

 

use print_r(); to test your arrays :

 

print_r($Function);

 

you'll see that key[0] has a value of : "0000","0010","9000" .. easiest way to add values (when on the fly), to the array is get your items and add them to a variable that will later be turned into an array, ie.

 

$arr[] = $cord;

and loop that for however many items you have .. then your array will set to go.

and your $Function is not the array you think it is.

 

use print_r(); to test your arrays :

 

print_r($Function);

 

you'll see that key[0] has a value of : "0000","0010","9000" .. easiest way to add values (when on the fly), to the array is get your items and add them to a variable that will later be turned into an array, ie.

 

$arr[] = $cord;

and loop that for however many items you have .. then your array will set to go.

 

Not quite cos that will guarentee the $cord is in the array when it might not be, only certain coordinates are to be in this array the 3 i gave in the example above are just to show you what im trying to do.. which is build array list in a variable then use that variable as the array to check if $cord is in the array($Function).

The code I posted is correct - you must be implementing it in an erroneous manner.

 

I know, thats why im kinda confused why it error'd cos its correct..... but heres the script with the error :

 

 

<?php
//above here is html so unlikely to be affecting the error

$cord = "0000";
$Collect = mysql_query("SELECT Function FROM usercrimes_blocked WHERE FloorID='$LocationID'")
Or die(mysql_error());
$row = mysql_fetch_assoc($Collect);
$Function = $row['Function']; echo $Function;
//$Function shows  "1000", "0900", "0800", "0700", "0600", "0500"
$Array = explode("," $Function);
If(in_array($cord,$Array)){echo $cord;}Else{     //THIS IS LINE 298
	echo '<input type="checkbox" name="list[]" value="'.$cord.'">';
}	
echo '</center>';
?>

Error:

Parse error: parse error on line 298

explode returns and array so you don't need to declare it one.

 

Just use what jack said:

 

$Array = explode(',' $Function);

 

I am but i get a parse error from it for some reason :S

 

You are still missing a comma ,have to give it like

 

$Array = explode(',',$Function);

and your $Function is not the array you think it is.

 

use print_r(); to test your arrays :

 

print_r($Function);

 

you'll see that key[0] has a value of : "0000","0010","9000" .. easiest way to add values (when on the fly), to the array is get your items and add them to a variable that will later be turned into an array, ie.

 

$arr[] = $cord;

and loop that for however many items you have .. then your array will set to go.

 

Not quite cos that will guarentee the $cord is in the array when it might not be, only certain coordinates are to be in this array the 3 i gave in the example above are just to show you what im trying to do.. which is build array list in a variable then use that variable as the array to check if $cord is in the array($Function).

i just threw $cord in there as an example for adding values to an array using [] .. $cord represents the string you are adding .. that's all.  i post what i know, you play around with it.
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.