Jump to content

re-assign array


Alicia

Recommended Posts

Hi guys,

 

Can somebody give me an idea how to assign the arrays I assigned in the for loop and the re-assign to another array as the script below ?

 


     <?PHP  
$ct1=mysql_query("SELECT id FROM `num` ORDER BY `id` DESC LIMIT 0,1")or die (mysql_error());
$ct2=mysql_fetch_array($ct1);
  
for ( $ss=$ct2['id']+1; $ss--
{
$cons_mag1=mysql_query("SELECT draw_id FROM `num` WHERE `id` = '$ss' ")or die (mysql_error());
$cons_mag2=mysql_fetch_array($cons_mag1);
$cons_mag3 = mysql_query("SELECT * FROM  `draw_doc` WHERE `type` = 'num' AND `draw_id` = '{$cons_mag2['draw_id']}' AND number = '1111' ORDER BY  `time` DESC ");
$cons_mag4=mysql_num_rows($cons_mag3);
if ("$cons_mag4" == '0')
{
$tp[]=0;
}
else
{
$tp[]=1;
}
}  
      

  // assign values of $tp[] to array() below
        $SQLQuery = array($tp[0],$tp[1],$tp[2],$tp[3],$tp[4],$tp[5],$tp[6],$tp[7],$tp[8],$tp[9],$tp[10]);
        // $SQLQuery = array(1,1,1,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0); <- output something like this
        $max = 0;
        $cur = 0;
        $last = 0;
      ?> 

 

 

Then another question will be how can I actually count the same most time value 1 is displayed consecutively in the script below?

 

// just count max of number 1 only and ignore number 0 
        for ($i=0, $x=sizeof($SQLQuery); $i<$x; $i++) {
  $SQLQuery[$i] == $last ? $cur++ : $cur = 1;
        $last = $SQLQuery[$i];
        if($cur > $max) $max = $cur;
        }

        echo $max;

 

Your advise will be appreciated and thank you.

Link to comment
Share on other sites

I want to echo all the numbers in the array tp[] into array($tp) ? this is not working. the output should be something like array(1,1,1,0,0,0) and etc.

 

please advise.

 

you normally cant Echo an array, this requires special handling.

if u try to echo an array, all you get is 'Array'.

So I think you need a finer description of what you are trying to do.

if this is for an sql query, which I think it is given the name of the variable.

 

sql_query("INSERT INTO table (tp1,tp2...) VALUES (". implode(',',$SQLQuery) .")";

the other code seems find for finding max consecutive sequences

Link to comment
Share on other sites

Erm...I'm not sure where to start!

// assign values of $tp[] to array() below
$SQLQuery = array($tp[0],$tp[1],$tp[2],$tp[3],$tp[4],$tp[5],$tp[6],$tp[7],$tp[8],$tp[9],$tp[10]);
// $SQLQuery = array(1,1,1,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0); <- output something like this

 

Okay $tp is an array of one's and zeros and you seam you want to transform it into .. itself!..

 

Note: to see an arrays output, use can use print_r

print_r($tp);

Note#2: for nice formatting add

echo "<pre>";

before the print_r

 

as for question #2.. after reading the question 3 times i assume you mean this.

whats the most amount of consecutive occurrences of 1,

ie

$SQLQuery = array(1,1,1,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0); 

would be 6, script

<?php
$tp = array(1,1,1,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,1,0);
echo countMaxOcc($tp,1);

function countMaxOcc($tp, $s){
$t=0;$c=0;$p=true;
foreach($tp as $n){
	if($n==$s){
		$c=($p)?$c+1:1;
		$p=true;
	}else{
		$t=($c>$t)?$c:$t;
		$p=false;
	}
}
return $t;
}

 

Link to comment
Share on other sites

I want to echo all the numbers in the array tp[] into array($tp) ? this is not working. the output should be something like array(1,1,1,0,0,0) and etc.

 

please advise.

 

Like this?

$string = "array(";
foreach ($tp as $n)
   $string .= $n . ",";
$string[strlen($string)-1] = ")";
echo $string;

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.