An example output of the following code may be this:
39 48 39 12 17 39 12
code:
if (mysql_num_rows($filterResult)) {
while ($filterrow = mysql_fetch_array($filterResult)) {
$vidID = $filterrow['routineID'];
...
...
echo "<video id='video$vidID'></video>";
}
}
I need it to output like this:
39 48 39_1 12 17 39_2 12_1
Notice how value 39 occurs three times and value 12 occurs twice.
After the first occurence of a value I need it
formatted like ##_#.
Can you help me code this up?
I appreciate your time and assistnace -
Derek
Use array_count_values like this:-
$array = array(1, 38, 1, 38,35);
print_r(array_count_values($array));
Output:-
Array
(
[1] => 2
[38] => 2
[35] => 1
)
Apply some logic to achieve what you want.
if (mysql_num_rows($filterResult)) {
$tmp = array();
while ($filterrow = mysql_fetch_array($filterResult)) {
$vidID = $filterrow['routineID'];
if(isset($tmp[$vidID]) {
$tmp[$vidID] = $tmp[$vidID] + 1;
$vidID = $vidID . '_' . $tmp[$vidID];
} else {
$tmp[$vidID] = 0;
}
echo $vidID;
}
}
if (mysql_num_rows($filterResult)) {
while ($filterrow = mysql_fetch_array($filterResult)) {
$j=-1;
$vidID = $filterrow['routineID'];
$arr[]=$vidID;
for($i=0;$i<count($arr);$i++){
if($arr[$i]==$vidID)
$j++;
}
if($j==0)
$j="";
else $j="_".$j;
echo $vidID.$j;
}
}
Here is the code:
$array1 = array(39,48,39,12,17,39,12);
$array2 = array(39,48,39,12,17,39,12);
foreach($array1 as $value){
$counts1 = array_count_values($array1);
$counts2 = array_count_values($array2);
if(!empty($counts2[$value])){
if ($counts1[$value] > 1){
$count = $counts1[$value] - $counts2[$value] + 1;
if($count > 1) {
echo $value.'_'.($count - 1).' ';
} else {
echo $value.' ';
}
} else {
echo $value.' ';
}
array_shift($array2);
}
}
Here is the PHP fiddle
Related
I have a simple array $iteration=[0,1,2,3,4] and I am trying to build a function to increment it to a max value of $max=12 for example and I can't reuse the same number for 2 keys. But so far I have very little success. Here is what I have now.
//$iteration is my array and $max is the maximum value a key can have.
IncrementIteration($iteration,$max){
$count=count($iteration);
while($count > 0){
if( ($iteration[($count-1)] < $max) ){
$iteration[($count-1)]++;
break;
}
$count--;
}
return $iteration;
}
But this never resets the keys that follows the incremented key and does not take into account if the number has already been used.
Here is what I am looking for as example of results:
print_r(IncrementIteration([0,1,2],12))
Output : Array ( [0] => 0 [1] => 1 [2] => 3 )
print_r(IncrementIteration([0,1,12],12))
Output : Array ( [0] => 1 [1] => 2 [2] => 3 )
print_r(IncrementIteration([0,11,12],12))
Output : Array ( [0] => 1 [1] => 2 [2] => 3 )
This would be the highest possible incrementation.
print_r(IncrementIteration([10,11,12],12))
Output : Array ( [0] => 10 [1] => 11 [2] => 12 )
Thanks for any help on this code.
I am adding the other functions to add more clarity about the purpose of this function.
function ReverseSUM($value,$array){
global $debug;
$count=count($array);
$count=3;
$values=array();
while($count > 0){
//Init of While Iteration
$iteration=GenerateIteration($count);
//We iterate
while(SumIteration($iteration,$array) != $value){
if($iteration === IncrementIteration($iteration,(count($array)-1))){
break;
} else {
$iteration=IncrementIteration($iteration,(count($array)-1));
}
//End of While Iteration
}
//End of While Iteration
if(SumIteration($iteration,$array) == $value){
array_push($values,$iteration);
}
unset($iteration);
if($debug){echo "</div>";};
$count--;
}
return $values;
}
function GenerateIteration($number){
$iteration=array();
$count = 0;
while($count < $number){
array_push($iteration,$count);
$count++;
}
return $iteration;
}
function IncrementIteration($iteration,$max){
$count=count($iteration);
while($count > 0){
if( ($iteration[($count-1)] < $max) ){
$iteration[($count-1)]++;
break;
}
$count--;
}
return $iteration;
}
function SumIteration($iteration,$array){
$result=array();
foreach($iteration as $key){
array_push($result,$array[$key]);
}
return array_sum($result);
}
May be some thing like this can help,
function unique_keys_array($array) {
$temp_array = array();
$i = 0;
$key_array = array();
foreach($array as $key) {
if (!in_array($key, $key_array)) {
$key_array[$i] = $key;
$temp_array[$i] = $key;
}
$i++;
}
return $temp_array;
}
print_r(unique_keys_array([1,2,2,3,4,5,6,7,8,8,9,9]));
returns Array
(
[0] => 1
[1] => 2
[3] => 3
[4] => 4
[5] => 5
[6] => 6
[7] => 7
[8] => 8
[10] => 9
)
Here is my final code for my Reverse Sum
function ReverseSUM($value,$array){
ini_set('max_execution_time', 10);
if (!function_exists('GenerateIteration')) {
function GenerateIteration($number){
global $debug;
$iteration=array();
$count = 0;
while($count < $number){
$count++;
array_push($iteration,$count);
}
return $iteration;
}
}
if (!function_exists('IncrementIteration')) {
function IncrementIteration($iteration,$max){
global $debug;
$count=count($iteration);
while($count > 0){
if( $iteration[($count-1)] < $max ){
$iteration[($count-1)]++;
if($count != count($iteration)){
$count2=$count;
while($count2 <= count($iteration)){
if($count2 != count($iteration)){
// if( ($iteration[$count2] < $max) ){
$iteration[$count2]=($iteration[($count2-1)]+1);
// }
}
$count2++;
}
}
break;
}
$max--;
$count--;
}
return $iteration;
}
}
if (!function_exists('SumIteration')) {
function SumIteration($iteration,$array){
global $debug;
$result=array();
foreach($iteration as $key){
array_push($result,$array[$key]);
}
return array_sum($result);
}
}
$count=count($array);
$count=3;
$values=array();
while($count > 0){
//Init of While Iteration
$iteration=GenerateIteration($count);
//We iterate
while(SumIteration($iteration,$array) != $value){
if($iteration === IncrementIteration($iteration,(count($array)-1))){
break;
} else {
$iteration=IncrementIteration($iteration,(count($array)-1));
}
//End of While Iteration
}
//End of While Iteration
if(SumIteration($iteration,$array) == $value){
array_push($values,$iteration);
}
unset($iteration);
$count--;
}
return $values;
}
And here is how I display the results:
<?php foreach($recap as $line => $value){ ?>
<?php if($line<2){?>
<table border="1">
<tr>
<th colspan="2" style="text-align:left;">Line <?=$line?> - <?=$value?></th>
</tr>
<tr>
<th>Iteration</th>
<th>Values</th>
</tr>
<?php foreach(ReverseSUM($value,$invoice) as $iteration => $values){?>
<tr>
<td><?=$iteration?></td>
<td>
<?php foreach($values as $array){?>
<?=($array +1)?><br />
<?php } ?>
</td>
</tr>
<?php } ?>
</table>
<?php } ?>
<?php } ?>
The $recap array simply contains the total values we are searching for. And the $invoice array contains all the invoice lines totals.
Code available on GitHub : https://github.com/LouisOuellet/ReverseSUM
Sheers
I've this code and I don't understand the output. I was expecting an output 121.
I looking to understand this specific function and not the factorial function.
Code:
function a($num) {
static $totFac = 1;
if($num > 0){
$totFac = $totFac * $num;
a($num - 1);
}
$totFac++;
return $totFac;
}
$result = a(5);
echo 'result: '.$result;
Output:
126
First of all, the factorial of 5 is 120 explained as:
5! = 5 x 4 x 3 x 2 x 1 = 120
Second, this is how a factorial should look like with recursive call:
function factorial($number) {
if ($number < 2) {
return 1;
} else {
return ($number * factorial($number-1));
}
}
If you don't mean factorial, then this is how it happens if I alter your function
function a($num) {
static $totFac = 1;
if($num > 0){
$totFac = $totFac * $num;
echo "calc totFac: " . $totFac . "\n";
a($num - 1);
}
$totFac++;
echo "increment totFac: " . $totFac . "\n";
return $totFac;
}
$result = a(5);
echo 'result: '.$result;
This is the output of the echo
calc totFac: 5
calc totFac: 20
calc totFac: 60
calc totFac: 120
calc totFac: 120
increment totFac: 120
increment totFac: 121
increment totFac: 122
increment totFac: 123
increment totFac: 124
increment totFac: 125
result: 126
<?php
function a($num) {
static $totFac = 1;
if($num > 0){
$totFac = $totFac * $num;
echo 'totfac: '.$totFac . "<br>";
return a($num - 1); //you have to return here to stop execution
} else {
$totFac++;
return $totFac;
}
}
$result = a(5);
echo 'result: '.$result;
?>
Look at the comment for explaination
I have a little problem with an array.
I have an array which looks like this:
Array
(
[start] => Array
(
[0] => 15168
)
[ende] => Array
(
[0] => 43
)
[string] => Array
(
[0] => 1050
)
)
The number I need is 1050. So I get it like this:
$number = $tabelle['string'][0];
Now my problem is that I can't use it in calculations. I already tried to convert it into an integer with the following line:
$number = intval($tabelle['string'][0]);
But this doesn't work. I always get 0 for $number. How to do it properly? I already searched on Google for about 2 hours.
Best regards
My whole script:
<?php
class data_pars {
var $datei;
var $read_laenge = 2000;
var $result;
function set_datei($datei) {
$this->datei = $datei;
}
function read($start,$ende) {
$file = #fopen ($this->datei,"r");
while (!feof($file)) {
$inhalt .= fgets($file,$this->read_laenge);
}
if(!$start) $start = 0;
if(!$ende) $ende = strlen($inhalt);
if($ende > strlen($inhalt)) $ende = strlen($inhalt);
$this->result = substr($inhalt,$start,$ende);
}
function get_result() {
return $this->result;
}
function get_in_out($in,$out,$in_out) {
$anzahl_ende = strlen($out);
$anzahl_start = strlen($in);
$start = 0;
$anzahl = substr_count($this->result, $in);
$count = 0;
if(!$in_out) {
$ad_start = $anzahl_start;
$ad_ende = $anzahl_ende;
}
while($count < $anzahl) {
$ar_start = strpos($this->result, $in, $start);
$ar_ende = strpos($this->result, $out, $ar_start + $anzahl_start);
$ar_string = substr($this->result,$ar_start + $ad_start, $ar_ende - $ar_start + $anzahl_ende - $ad_ende - $ad_start);
$output[start][] = $ar_start;
$output[ende][] = $ar_ende - $ar_start + $anzahl_ende;
$output[string][] = trim($ar_string);
$start = $ar_start + $anzahl_start;
$count++;
}
return $output;
}
}
$data = new data_pars();
$data->set_datei('http://www.elitepvpers.com/theblackmarket/profile/6005376');
$data->read('0','20000');
$tabelle = $data->get_in_out('<td>elite*gold:</td>','</td>',false);
$number = intval($tabelle['string'][0]);
echo '<pre>';
print_r($tabelle);
echo '</pre>';
echo $number;
?>
So I always get 0 for $number instead of 1050;
You did 2 main errors:
1. You have to initialize $inhalt before your while loop like this:
$inhalt = "";
while (!feof($file)) {
$inhalt .= fgets($file,$this->read_laenge);
}
2. You forgot to put quotes around the indexes:
$output["start"][] = $ar_start;
$output["ende"][] = $ar_ende - $ar_start + $anzahl_ende;
$output["string"][] = trim($ar_string);
And now why did your intval() failed? Because if you right click and show source you will see in the array the value is:
<td>1050
So this can't convert to a int! And the <td> you don't see, because it's a html tag
Now you can easy extract only the number with this lines:
$tabelle["string"]["0"] = filter_var($tabelle["string"]["0"], FILTER_SANITIZE_NUMBER_INT);
echo $number = intval($tabelle["string"]["0"]);
Output:
1050
If I understand your code and logic correctly.
try replace:
$output[start][] = $ar_start;
$output[ende][] = $ar_ende - $ar_start + $anzahl_ende;
$output[string][] = trim($ar_string);
to this:
$
output['start'][] = intval($ar_start);
$output['ende'][] =intval( $ar_ende - $ar_start + $anzahl_ende);
$output['string'][] = intval(trim($ar_string));
I want to display the output as like this
1 1
12 21
123 321
1234 4321
1234554321
Here is my php code
for($i=1;$i <= 5;$i++)
{
for($j=1;$j<=$i;$j++)
{
// print the result
echo "$j";
}
for($y=$i;$y<=$i;$y++)
{
echo ' ';
}
for($k=$i;$k>=1;$k--)
{
// print the result
echo " $k";}
echo "<br/>";
}
But I got the output like this
1 1
12 2 1
123 3 2 1
1234 4 3 2 1
12345 5 4 3 2 1
Please help me get an output like above.
try
for($i=1;$i <= 5;$i++) {
for($j=1;$j<=$i;$j++) {
echo "$j";
}
for($y=0;$y<(5-$i)*4;$y++) {
echo ' ';
}
for($l=$i;$l>0;$l--) {
echo "$l";
}
echo "<br/>";
}
output:-
1 1
12 21
123 321
1234 4321
1234554321
For browser view :- for($y=0;$y<(5-$i)*4;$y++) else correct way to going is for($y=0;$y<(5-$i)*2;$y++)
Did you ever here about the PHP functions str_repeat and range?
http://php.net/manual/en/function.str-repeat.php.
http://php.net/manual/en/function.range.php
With it you can print your character like this:
echo str_repeat(' ', (5 -$i) *4);
Complete code
$count = 5; // count oft rows and oft iterated numbers
for ($i = 1; $i <= $count; $i++) {
echo implode('', range(1, $i));
echo str_repeat(' ', ($count -$i) *4);
echo implode('', array_reverse(range(1, $i)));
echo '<br />';
}
How about this?
for($i=1;$i<=5;$i++){
echo substr("12345", 0, $i);
echo str_repeat(5-$i, ' ');
echo str_reverse(substr("12345", 0, $i));
echo '<br>';
}
Try this:
for($i=1;$i <= 5;$i++)
{
for($j=1;$j<=$i;$j++)
{
echo "$j";
}
for($y=1;$y<=10-(2*$i);$y++)
{
echo ' ';
}
for($k=$i;$k>=1;$k--)
{
// print the result
echo "$k";
}
echo "<br/>";
}
OUTPUT:
1 1
12 21
123 321
1234 4321
1234554321
How about this with $y<= 2 * (5 - i),
for($i=1;$i <= 5;$i++) {
for($j=1;$j<=$i;$j++) {
echo "$j";
}
for($y=1;$y<= 2 * (5 - i); $y++) {
echo ' ';
}
for($k=$i;$k>=1;$k--) {
echo "$k";
}
echo "<br/>";
}
for ($counter=1;$counter<= 5;$conter++)
{
for ($j=1;$j<=$counter;$j++)
{
echo "$j";
}
for ($y=1;$y<=10-(2*$counter);$y++)
{
echo ' ';
}
for ($k=$counter;$k>=1;$k--)
{
echo "$k";
}
echo "<br/>";
}
I have an array:
Array
(
[red] => 252
[green] => 168
[blue] => 166
[alpha] => 0
)
It's an output of function imagecolorsforindex.
How can I get a HTML code from these elements? For example: #99CCFF
Strictly speaking you can't, since alpha is not supported. But since the alpha is 0, we can assume that it won't matter. As such, pass each value into sprintf() with a format specifier of %02x for each element.
c = sprintf('#%02x%02x%02x', val['red'], val['green'], val['blue']);
PHP Convert RGB from/to HTML hex color
rgb2html($array[0], $array[1], $array[2])
There's a function contributed in the comments of this page of the PHP manual.
<?PHP
function rgb2hex2rgb($c){
if(!$c) return false;
$c = trim($c);
$out = false;
if(preg_match("/^[0-9ABCDEFabcdef\#]+$/i", $c)){
$c = str_replace('#','', $c);
$l = strlen($c) == 3 ? 1 : (strlen($c) == 6 ? 2 : false);
if($l){
unset($out);
$out[0] = $out['r'] = $out['red'] = hexdec(substr($c, 0,1*$l));
$out[1] = $out['g'] = $out['green'] = hexdec(substr($c, 1*$l,1*$l));
$out[2] = $out['b'] = $out['blue'] = hexdec(substr($c, 2*$l,1*$l));
}else $out = false;
}elseif (preg_match("/^[0-9]+(,| |.)+[0-9]+(,| |.)+[0-9]+$/i", $c)){
$spr = str_replace(array(',',' ','.'), ':', $c);
$e = explode(":", $spr);
if(count($e) != 3) return false;
$out = '#';
for($i = 0; $i<3; $i++)
$e[$i] = dechex(($e[$i] <= 0)?0:(($e[$i] >= 255)?255:$e[$i]));
for($i = 0; $i<3; $i++)
$out .= ((strlen($e[$i]) < 2)?'0':'').$e[$i];
$out = strtoupper($out);
}else $out = false;
return $out;
}
?>
Output
#FFFFFF =>
Array{
red=>255,
green=>255,
blue=>255,
r=>255,
g=>255,
b=>255,
0=>255,
1=>255,
2=>255
}
#FFCCEE =>
Array{
red=>255,
green=>204,
blue=>238,
r=>255,
g=>204,
b=>238,
0=>255,
1=>204,
2=>238
}
CC22FF =>
Array{
red=>204,
green=>34,
blue=>255,
r=>204,
g=>34,
b=>255,
0=>204,
1=>34,
2=>255
}
0 65 255 => #0041FF
255.150.3 => #FF9603
100,100,250 => #6464FA
You can try this simple piece of code below.
$rgb = (123,222,132);
$rgbarr = explode(",",$rgb,3);
echo sprintf("#%02x%02x%02x", $rgbarr[0], $rgbarr[1], $rgbarr[2]);
This will return #7bde84