EchoFool Posted April 10, 2009 Share Posted April 10, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/153486-solved-array-variable-help/ Share on other sites More sharing options...
mrMarcus Posted April 10, 2009 Share Posted April 10, 2009 for starters, where are you defining $cord? Quote Link to comment https://forums.phpfreaks.com/topic/153486-solved-array-variable-help/#findComment-806416 Share on other sites More sharing options...
EchoFool Posted April 10, 2009 Author Share Posted April 10, 2009 $cord is before this particular section, $cord is the coordinate of the user's position which is a 4 digit number. In this situation $cord is set to 0000. Quote Link to comment https://forums.phpfreaks.com/topic/153486-solved-array-variable-help/#findComment-806419 Share on other sites More sharing options...
Maq Posted April 10, 2009 Share Posted April 10, 2009 You're not creating the array correctly. $Array = Array("0000", "0010", "9000"); Before you were just giving it 1 element [0] => '"0000", "0010", "9000"' Quote Link to comment https://forums.phpfreaks.com/topic/153486-solved-array-variable-help/#findComment-806421 Share on other sites More sharing options...
mrMarcus Posted April 10, 2009 Share Posted April 10, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/153486-solved-array-variable-help/#findComment-806423 Share on other sites More sharing options...
EchoFool Posted April 10, 2009 Author Share Posted April 10, 2009 Yeh but that doesn't allow me to add an array from the $Function variable which is what I need to do because I build the list of numbers into the variable which then becomes the array. Quote Link to comment https://forums.phpfreaks.com/topic/153486-solved-array-variable-help/#findComment-806425 Share on other sites More sharing options...
EchoFool Posted April 10, 2009 Author Share Posted April 10, 2009 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). Quote Link to comment https://forums.phpfreaks.com/topic/153486-solved-array-variable-help/#findComment-806426 Share on other sites More sharing options...
jackpf Posted April 10, 2009 Share Posted April 10, 2009 $Array = explode(',' $Function); Quote Link to comment https://forums.phpfreaks.com/topic/153486-solved-array-variable-help/#findComment-806427 Share on other sites More sharing options...
EchoFool Posted April 10, 2009 Author Share Posted April 10, 2009 $Array = explode(',' $Function); What does that do ? Quote Link to comment https://forums.phpfreaks.com/topic/153486-solved-array-variable-help/#findComment-806430 Share on other sites More sharing options...
jackpf Posted April 10, 2009 Share Posted April 10, 2009 It creates an array of $Function, splitting it up on the comma punctuation sign. Quote Link to comment https://forums.phpfreaks.com/topic/153486-solved-array-variable-help/#findComment-806434 Share on other sites More sharing options...
EchoFool Posted April 10, 2009 Author Share Posted April 10, 2009 Oh right i thought function array would need to be included like : $Array = array(explode(',' $Function)); ? Though i get a parse error from $Array = explode(',' $Function); :S Quote Link to comment https://forums.phpfreaks.com/topic/153486-solved-array-variable-help/#findComment-806448 Share on other sites More sharing options...
Maq Posted April 10, 2009 Share Posted April 10, 2009 explode returns and array so you don't need to declare it one. Just use what jack said: $Array = explode(',' $Function); Quote Link to comment https://forums.phpfreaks.com/topic/153486-solved-array-variable-help/#findComment-806450 Share on other sites More sharing options...
EchoFool Posted April 10, 2009 Author Share Posted April 10, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/153486-solved-array-variable-help/#findComment-806455 Share on other sites More sharing options...
Maq Posted April 10, 2009 Share Posted April 10, 2009 Can you post your current code and the EXACT error? Quote Link to comment https://forums.phpfreaks.com/topic/153486-solved-array-variable-help/#findComment-806466 Share on other sites More sharing options...
jackpf Posted April 10, 2009 Share Posted April 10, 2009 The code I posted is correct - you must be implementing it in an erroneous manner. Quote Link to comment https://forums.phpfreaks.com/topic/153486-solved-array-variable-help/#findComment-806470 Share on other sites More sharing options...
EchoFool Posted April 10, 2009 Author Share Posted April 10, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/153486-solved-array-variable-help/#findComment-806478 Share on other sites More sharing options...
jackpf Posted April 10, 2009 Share Posted April 10, 2009 Hmm...you could try neatening up your if() statements - could be useful. Try this: If(in_array($cord,$Array)) { echo $cord; } Else { echo '<input type="checkbox" name="list[]" value="'.$cord.'">'; } Quote Link to comment https://forums.phpfreaks.com/topic/153486-solved-array-variable-help/#findComment-806485 Share on other sites More sharing options...
Maq Posted April 10, 2009 Share Posted April 10, 2009 Missing a comma here: $Array = explode(",", $Function); Quote Link to comment https://forums.phpfreaks.com/topic/153486-solved-array-variable-help/#findComment-806488 Share on other sites More sharing options...
sandeep529 Posted April 10, 2009 Share Posted April 10, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/153486-solved-array-variable-help/#findComment-806493 Share on other sites More sharing options...
jackpf Posted April 10, 2009 Share Posted April 10, 2009 Ahhh didn't notice that - that was actually in the code I posted. I apologise profusely Funny how no one noticed it though... Quote Link to comment https://forums.phpfreaks.com/topic/153486-solved-array-variable-help/#findComment-806494 Share on other sites More sharing options...
Maq Posted April 10, 2009 Share Posted April 10, 2009 Is there an echo in here...? Quote Link to comment https://forums.phpfreaks.com/topic/153486-solved-array-variable-help/#findComment-806495 Share on other sites More sharing options...
jackpf Posted April 10, 2009 Share Posted April 10, 2009 :-X Quote Link to comment https://forums.phpfreaks.com/topic/153486-solved-array-variable-help/#findComment-806498 Share on other sites More sharing options...
mrMarcus Posted April 10, 2009 Share Posted April 10, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/153486-solved-array-variable-help/#findComment-806502 Share on other sites More sharing options...
EchoFool Posted April 10, 2009 Author Share Posted April 10, 2009 Thanks for the help everyone Works now Quote Link to comment https://forums.phpfreaks.com/topic/153486-solved-array-variable-help/#findComment-806530 Share on other sites More sharing options...
jackpf Posted April 10, 2009 Share Posted April 10, 2009 Good stuff. Quote Link to comment https://forums.phpfreaks.com/topic/153486-solved-array-variable-help/#findComment-806536 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.