Alicia Posted January 12, 2010 Share Posted January 12, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/188221-re-assign-array/ Share on other sites More sharing options...
lemmin Posted January 12, 2010 Share Posted January 12, 2010 I don't think I understand. If you want to copy all of the values of an array to another array, you can just assign it: $SQLQuery = $tp; If that isn't what you mean, could you explain more of what you are trying to accomplish? Quote Link to comment https://forums.phpfreaks.com/topic/188221-re-assign-array/#findComment-993844 Share on other sites More sharing options...
Alicia Posted January 13, 2010 Author Share Posted January 13, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/188221-re-assign-array/#findComment-993979 Share on other sites More sharing options...
Sesquipedalian Posted January 13, 2010 Share Posted January 13, 2010 Your code was written kind of odd, but this is what I think you're asking for: <?php $i=0; foreach($tp as $val) { $SQLQuery[$i] = $val; $i++; } ?> Am I missing a part of what you're looking for? Quote Link to comment https://forums.phpfreaks.com/topic/188221-re-assign-array/#findComment-994005 Share on other sites More sharing options...
laffin Posted January 13, 2010 Share Posted January 13, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/188221-re-assign-array/#findComment-994018 Share on other sites More sharing options...
MadTechie Posted January 13, 2010 Share Posted January 13, 2010 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; } Quote Link to comment https://forums.phpfreaks.com/topic/188221-re-assign-array/#findComment-994026 Share on other sites More sharing options...
lemmin Posted January 14, 2010 Share Posted January 14, 2010 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; Quote Link to comment https://forums.phpfreaks.com/topic/188221-re-assign-array/#findComment-995138 Share on other sites More sharing options...
MadTechie Posted January 14, 2010 Share Posted January 14, 2010 If thats the case one line should do it! echo "array(".implode(",",$tp).")"; Quote Link to comment https://forums.phpfreaks.com/topic/188221-re-assign-array/#findComment-995152 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.