Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/208600-can-i-use-array-like-this/
Share on other sites

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);

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

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

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

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)

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']

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

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.