karimali831 Posted July 22, 2010 Share Posted July 22, 2010 Hi I'm completely new to arrays so wanted to ask if I can use an array like this: while loop { $clan1 = '"'.$dm['clan1'].'", '; echo $clan1; } $clan1 echo = "353", "353", "76", "353", "241", "129", "297", "353", "353", "353", $array = array($clan1); or have I gone across the wrong idea? I want to put all of $dm['clan1'] into array basically. Quote Link to comment Share on other sites More sharing options...
joel24 Posted July 22, 2010 Share Posted July 22, 2010 use a foreach loop, http://php.net/manual/en/control-structures.foreach.php i.e. $array = array(1,2,3,4,5); foreach ($array as $value) { echo $value . ','; } or you can implode the array with a comma and space between each index value. $array = array(1,2,3,4,5); echo implode(", ", $array); Quote Link to comment Share on other sites More sharing options...
karimali831 Posted July 23, 2010 Author Share Posted July 23, 2010 Thanks but my only issue is the values that are in the array. For example, $var = ''.$dm['clan1'].', '; $array = array($var); echo implode(", ", $array); If I echo $var, I get : 365, 249, 353, 88, 353, 353, 353, 72, 382, 154, so... $array = array(365, 249, 353, 88, 353, 353, 353, 72, 382, 154, ); question is will $array work even though there is a comma after the last value? if not, what else can I do to use all rows for $dm['clan1'] in the array? Thanks alot for your help Quote Link to comment Share on other sites More sharing options...
.josh Posted July 23, 2010 Share Posted July 23, 2010 yes you can create an array like that, with a comma after the last value. What is actually happening is you are assigning a null value to the element after that last value, which basically negates itself. However, I have no idea what you are actually trying to accomplish in your code specifically... Quote Link to comment Share on other sites More sharing options...
karimali831 Posted July 23, 2010 Author Share Posted July 23, 2010 Take a look at the full code: $cupmatches = safe_query("SELECT * FROM ".PREFIX."cup_matches WHERE (clan1='$clanID' || clan2='$clanID') AND (score1 != '0' || score2 != '0') AND (clan1 != '0' AND clan2 != '0') AND (clan1 != '2147483647' AND clan2 != '2147483647')"); while($dm=mysql_fetch_array($cupmatches)) { $clan1 = ''.$dm['clan1'].', '; $clan2 = ''.$dm['clan2'].', '; $array1 = array($clan1); echo implode(", ", $array1); $array2 = array($clan2); echo implode(", ", $array2); if($clanID==$array1) { $cupmatches = safe_query("SELECT SUM(score2) as lostpoints FROM ".PREFIX."cup_matches WHERE clan1='$clanID' AND (clan1 != '0' AND clan2 != '0') AND (clan1 != '2147483647' AND clan2 != '2147483647')"); $ds=mysql_fetch_array($cupmatches); $lostpoints = $ds['lostpoints']; echo "lostpoints = $lostpoints"; }if($clanID==$array2){ $cupmatches2 = safe_query("SELECT SUM(score1) as lostpoints2 FROM ".PREFIX."cup_matches WHERE clan2='$clanID' AND (clan1 != '0' AND clan2 != '0') AND (clan1 != '2147483647' AND clan2 != '2147483647')"); $ds=mysql_fetch_array($cupmatches2); $lostpoints2 = $ds['lostpoints2']; echo "lostpoints2 = $lostpoints2"; } problem now falls under if($clanID==$array1) $clanID is 353 and 353 is in $array1 and it does not echo $lostpoints Quote Link to comment Share on other sites More sharing options...
karimali831 Posted July 23, 2010 Author Share Posted July 23, 2010 Tried: echo $clans1 = 353, 353, 76, 353, 241, 129, 297, 353, 353, 353, $array1 = array($clan1); $clans1 = implode(", ", $array1); if(353==$clans1) {die('test');} why does not it not die? 353 does equal 353 in array.. ($clans1) Quote Link to comment Share on other sites More sharing options...
karimali831 Posted July 23, 2010 Author Share Posted July 23, 2010 Managed to do something else with my imagination: $valid = ''.$clanID.'=='.$dm['clan1'].' ||'; if($valid) { } and works Quote Link to comment Share on other sites More sharing options...
.josh Posted July 23, 2010 Share Posted July 23, 2010 umm... I don't think that's doing what you think it is doing...all you are doing is setting a regular string value to $valid, and the if() condition just checks if $valid exists, which it does. It's not actually comparing $clanID to $dm['clan1'] Quote Link to comment Share on other sites More sharing options...
karimali831 Posted July 23, 2010 Author Share Posted July 23, 2010 All I was looking for is if $clanID equals one of the clanID at $dm['clan1'] and it worked perfectly. So like this: $valid = ''.$clanID.'=='.$dm['clan1'].' ||'; if($clanID == 353 || $clanID == 262 || $clanID == 545) and so on... Quote Link to comment Share on other sites More sharing options...
.josh Posted July 23, 2010 Share Posted July 23, 2010 no, that did not work perfectly. All that condition does is check if $valid exists. It does not compare the variable to what's in the array. As far as the condition is concerned, it's just an arbitrary string. your condition is basically the same as if (isset($valid)) If you think that it is doing what you think it is doing, then try this: $clanID = 5; $dm = array('clan1'=>1,'clan2'=>2,'clan3'=>3); $valid = ''.$clanID.'=='.$dm['clan1'].' ||'; if($valid) { echo $clanID . " is in array! wait...."; } If you want to check if a value is in an array, this is what you would do (example): $array = array(1,2,3,4,5); $value = 3; if (in_array($value,$array)) { // the value of $value is in the array $array } else { // it is not Quote Link to comment 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.