Hey i have array calculation where i need to refactor this code using loops please suggest...
<?php
arr = [286538,237034,192724,150815,117523,82754,49707];
$a1 = $arr[0]/7;
$a2 = ($a1+$arr[1])/7;
$a3 = ($a1+$a2+$arr[2])/7;
$a4 = ($a1+$a2+$a3+$arr[3])/7;
$a5 = ($a1+$a2+$a3+$a4+$arr[4])/7;
$a6 = ($a1+$a2+$a3+$a4+$a5+$arr[5])/7;
$a7 = ($a1+$a2+$a3+$a4+$a5+$a6+$arr[6])/7;
?>
Output -
40934
39709.714285714
39052.530612245
38644.463556851
39409.10120783
40071.972808949
41075.540353084
Here is how you can utilize loops in your problem:
$arr = [286538,237034,192724,150815,117523,82754,49707];
$newArr = [];
$counter = 0;
while($counter < count($arr)){
$sum = array_sum($newArr);
$newArr[] = ($sum + $arr[$counter]) / 7;
$counter++;
}
<?php
$arr = [286538,237034,192724,150815,117523,82754,49707];
function calcarr($n){
$result = 0;
global $arr;
if ($n <= 0) return $result;
for ($i=0;$i<$n;$i++) $result += calcarr($i);
return ($result + $arr[$n-1])/7;
}
$a1 = $arr[0]/7;
$a2 = ($a1+$arr[1])/7;
$a3 = ($a1+$a2+$arr[2])/7;
$a4 = ($a1+$a2+$a3+$arr[3])/7;
$a5 = ($a1+$a2+$a3+$a4+$arr[4])/7;
$a6 = ($a1+$a2+$a3+$a4+$a5+$arr[5])/7;
$a7 = ($a1+$a2+$a3+$a4+$a5+$a6+$arr[6])/7;
echo "$a7\n";
echo calcarr(7)."\n";
?>
RESULT:
41075.540353084
41075.540353084
<?php
$arr = [286538,237034,192724,150815,117523,82754,49707];
$result = [];
foreach($arr as $k => $item)
$result["a$k"] = (float) (array_sum($result) + $item)/7;
var_dump($result);
Output:
array(7) {
["a0"]=>
float(40934)
["a1"]=>
float(39709.714285714)
["a2"]=>
float(39052.530612245)
["a3"]=>
float(38644.463556851)
["a4"]=>
float(39409.10120783)
["a5"]=>
float(40071.972808949)
["a6"]=>
float(41075.540353084)
}
If you want the variables as you have listed:
extract($result);
If you don't care about keys and to extract those variables, this will just give you a 0 indexed array as for your result:
foreach($arr as $item)
$result[] = (float) (array_sum($result) + $item)/7;
Related
The contents of this question have been removed due to a DMCA Takedown request by Codility Limited.
Here is the Simplets PHP solution for the Above question from the Codility test.
<?php
$A = [2,2,1,2];
$B = [1,3,4,4];
$w = [3,5,2,4,1];
$N = 5;
// $A = [1];
// $B = [3];
// $A = [1,3];
// $B = [2,4];
function solution ($N, $A, $B){
if(count($A) != count($B) || !is_int($N) )
{
return false;
}
$V = [];
$vertextCount = [];
foreach($A as $k=>$val){
if(!isset($vertextCount[$val])){
$vertextCount[$val] = 0;
}
$vertextCount[$val] += 1;
}
foreach($B as $k=>$val){
if(!isset($vertextCount[$val])){
$vertextCount[$val] = 0;
}
$vertextCount[$val] += 1;
}
if($vertextCount < $N)
{
$vertextCount[$N] = 0;
}
$VC = $vertextCount;
$tn = $N;
$wightArr = [];
while(count($VC) > 0){
$maxKey = current(array_keys($VC, max($VC)));
$wightArr[$maxKey] = $tn;
unset($VC[$maxKey]);
$tn--;
}
$sum = 0;
foreach($A as $k=>$val){
$sum += $wightArr[$A[$k]] + $wightArr[$B[$k]];
}
return $sum;
}
echo $sum = solution($N, $A, $B);
NOTE:- Tested against the 3 given Examples in the test, Not sure about all the test cases.
I have 2 arrays in php
$array1[] = a,b,c,d,e;
$array2[] = 1,2,3,4,5;
$data = array('letter'=>$array1,'num'=>$array2);
return json_encode($data);
This will return:
[[a,b,c,d,e],[1,2,3,4,5]]
I'd like to return it in json_encode like this:
$data = [[1a,1],[b,2],[c,3],[d,4],[e,5]];
Can someone help me with this?
this is the simplest solution
$result = array();
foreach ($array1 as $k1 => $v1) {
$result[] = array($v1, $array2[$k1]);
}
echo json_encode($result)
but arrays must have the same length and same keys
Try below code, it is flexible and don't have to care about the length of the arrays.
<?php
$letters = array('a','b','c','d','e');
$numbers = array('1','2','3','4','5');
$counter = (sizeof($letters) > sizeof($numbers)) ? sizeof($letters) : sizeof($numbers);
$arr = array();
for($i=0; $i<$counter; $i++)
{
if(array_key_exists($i, $letters))
$arr[$i][] = $letters[$i];
if(array_key_exists($i, $numbers))
$arr[$i][] = $numbers[$i];
}
$json = json_encode($arr);
echo $json;
Output:
[["a","1"],["b","2"],["c","3"],["d","4"],["e","5"]]
Demo:
http://3v4l.org/7v7X4
What you are looking for is the function array_combine().
Here is an example:
$array1 = array("a","b","c","d","e");
$array2 = array(1,2,3,4,5);
$data = array_combine($array1, $array2);
$new_data = array();
foreach($data AS $key => $value) {
$new_data[] = array($key, $value);
}
print_r(json_encode($new_data));
Which should return something like:
[["a",1],["b",2],["c",3],["d",4],["e",5]]
UPDATE Changed the code to give the result wanted...
let me start off by saying what I have works just fine. However, I was wondering if there was a method using foreach loops, instead of while loops, that will return the same result. My current code is:
$a[0] = $_GET['a1'];
$a[1] = $_GET['a2'];
$a[2] = $_GET['a3'];
$a[3] = $_GET['a4'];
$a[4] = $_GET['a5'];
$a[5] = $_GET['a6'];
$a[6] = $_GET['a7'];
$a[7] = $_GET['a8'];
$a[8] = $_GET['a9'];
$a[9] = $_GET['a10'];
$a[10] = $_GET['a11'];
$a[11] = $_GET['a12'];
$a[12] = $_GET['a13'];
$a[13] = $_GET['a14'];
$a[14] = $_GET['a15'];
$a[15] = $_GET['a16'];
$a[16] = $_GET['a17'];
$a[17] = $_GET['a18'];
$a[18] = $_GET['a19'];
$a[19] = $_GET['a20'];
$a[20] = $_GET['a21'];
$a[21] = $_GET['a22'];
$a[22] = $_GET['a23'];
$a[23] = $_GET['a24'];
$a[24] = $_GET['a25'];
$a[25] = $_GET['a26'];
$a[26] = $_GET['a27'];
$a[27] = $_GET['a28'];
$array1 = array(
array(-0.0165333833412173,-0.805148897111146,-0.253228140499216,-0.184436610496828,-0.182460815146422,-0.562680616467753),
array(0.0530980021095432,0.197367160352892,0.486754591654999,0.100148472516492,0.129649075750223,-0.0891313538023273),
array(1.20820560236013,1.60297691019929,1.88645801788488,1.4097784563,1.35491780171462,1.83839262576814),
array(0.35474704442591,0.669288291278341,-0.618871707987998,0.314393917137463,0.369309934864932,0.302877707428276),
array(1.73737846681824,1.23045039205127,1.74985170545799,2.53444768504332,0.947425891979588,2.49693773110446),
array(1.08515864048978,1.19974869194067,1.40391203998614,0.645993089768699,1.18500365352365,0.183710871687018),
array(-0.158201130751605,0.245894811416122,-0.13491139550657,0.268781009345758,0.584094496717681,-0.475038989317082),
array(2.69120997158388,2.6335032877555,2.1678974119528,1.98159587601917,2.47871441914596,3.11218576826585),
array(1.58301428618536,1.4690581543491,1.64181831092486,1.31270093574087,0.956030437566211,1.68095459548238),
array(0.567053945109392,0.672686283651576,0.50520628014895,0.573859412747649,0.906581004455213,0.675935843127413),
array(2.21154924140175,2.4629227185282,2.44302564998387,1.76594656224375,2.24292394905905,2.46555856986067),
array(0.789058773638184,0.56494483459565,1.02220848055446,0.740827710977414,0.0568788365193514,0.974084436394248),
array(-1.22527151151768,-0.37116067796664,-0.993915606653743,-0.265414040283105,-0.0483919594705323,-1.26478189171631),
array(1.93198111668015,0.978691339072353,0.896606685235391,1.43344220993764,1.44428119515243,0.832846974841109),
array(0.0960968892129648,0.179429676667834,0.653561055735465,0.611111687937286,0.420917607921928,0.788124847059183),
array(-0.570188484564322,-0.367869613869776,-0.425708923999659,0.58428081962228,0.431696028180505,-0.560777966949268),
array(2.18753554884398,2.10453220450978,2.18396016604212,1.53663043726462,1.6128493800773,2.47023229651768),
array(0.933680338305853,0.701683572817621,1.05817969240128,1.13326689801564,1.06305023491714,1.28172788495342),
array(0.237030103809889,1.19429662809383,1.32352397170755,0.787441428409051,1.06861486730769,0.196323518345157),
array(1.27102015175144,0.999617071482296,1.14337292692749,1.79352497701309,1.14261585172827,1.18874166503307),
array(0.746730730970985,0.584005303699165,0.00845233248507581,0.617844244971367,0.646839413026708,0.393004935019083),
array(1.33877796293032,1.18403793988876,1.16686497194317,1.18487924665819,0.796233877768925,1.21668080067963),
array(1.66146357909026,1.52622605987311,1.51673796371559,2.45659237209795,1.58826974938031,1.87667392945756),
array(1.23205756178945,1.25461474916892,1.0738857243227,1.21949513612512,0.847503017550969,1.33075294766838),
array(0.976236852070675,1.0676137689421,1.08390543200419,1.19114160706157,0.804407032221587,0.918305581716723),
array(1.0514345299356,1.24825633668808,1.15750685444102,1.34538988886631,1.24490007119083,0.994386901437322),
array(-50.5445732202499,-53.6034355984409,-52.2301074110166,-59.4416059910645,-50.3559798265968,-51.5135149950383),
);
$array2 = array();
$x = 0;
while($x < COUNT($array1)){
$y = 0;
while($y < COUNT($array1[$x])){
$array2[$x][$y] = $array1[$x][$y] * $a[$x];
//echo($array2[$x][$y] . ', ');
$y++;
}
//echo('<br>');
$x++;
}
Again, while this works, I am trying to get into the habit of writing cleaner code. Additionally, I am still relatively new to PHP, so I'm still struggling a bit with some of its syntax and structuring.
Thanks in advance.
This should work identical to what you have.
$array2 = array();
foreach($a as $k1=>$a1)
{
foreach($array1[$k1] as $k2=>$a2)
{
$array2[$k1][$k2] = $a2 * $a1;
}
}
The basic format of a foreach loop is foreach($array as $key=>$value) if you don't need the array key, you can shorten it to foreach($array as $value).
You can reduce the code by directly accessing the $_GET values instead of copying them into $a.
$result = array();
foreach($array1 as $k1 => $array2) {
foreach($array2 as $k2 => $value) {
$result[$k1][$k2] = $value * $_GET['a'.($k1+1)];
}
}
Question
Currently My output is coming like this PNL testing 1,10,PNL testing 2,55, I want to manipulate it and store it in two different variables as a string like:
$teams = "PNL testing 1, PNL testing 2";
$amount = "10, 55";
Please let me know how should I split in the above format.
Code
while ($row1 = mysql_fetch_assoc($result)){
$profit = 0;
$total = 0;
$loss = 0;
$final_array = '';
$final_array .= $teams = $row1['t_name'].",";
$teams = explode(",", $teams);
$transact_money = $row1['pnl_amount'];
$pnl_results = $row1['pnl'];
$transact_money = explode("|", $transact_money);
$pnl_results = explode("|", $pnl_results);
for($i=0; $i<count($transact_money); $i++){
if($pnl_results[$i]=='Profit'){
$profit = $profit + $transact_money[$i];
}else{
$loss = $loss + $transact_money[$i];
}//end if
}//end for..
$team_profits = $profit - $loss.",";
$final_array .= $team_profits;
echo $final_array;
}
$s = "PNL testing 1,10,PNL testing 2,55";
$res = array();
$result = preg_match_all("{([\w\s\d]+),(\d+)}", $s, $res);
$teams = join(', ', $res[1]); //will be "PNL testing 1, PNL testing 2"
$amount = join(', ', $res[2]); //will be "10, 55"
$string = "PNL testing 1,10,PNL testing 2,55,";
preg_match_all("/(PNL testing \d+),(\d+),/", $string, $result);
$teams = implode(",", $result[1]);
$amount = implode(",", $result[2]);
Less fancy than RegExp, but more obvious:
$final_array = "PNL testing 1,10,PNL testing 2,55";
$final_array_array = explode(',', $final_array);
$teams = array();
$amount = array();
$index = 0;
foreach ($final_array_array as $item) {
switch ($index) {
case 0:
$teams[] = $item;
break;
case 1:
$amount[] = $item;
}
$index = 1-$index;
}
I've got field in my database which contain strings like 21;48;33;22;31. Then I'd like to convert it to mathematical calculation 21+48+33+22+31.
$points = "21;48;33;22;31";
$points = str_replace(";","+",$points );
$points = preg_replace('/[^0-9\+\-\*\/\s]+/', '', $points);
echo $points;
But that simply does not work. I've got the same string "21+48+33+22+31" instead of the sum.
$points = "21;48;33;22;31";
$points = explode(';',$points );
$points = array_sum($points);
echo $points;
$points = "21;48;33;22;31";
$arr = explode(";", $points);
$points = 0;
foreach($arr as $key => $rows){
$points += $rows[$key];
}
echo $points;
Try above code it will give you proper result.
or you can try it also:
$points = "21;48;33;22;31";
$arr = explode(";", $points);
echo $points = array_sum($arr);
The easiest way is to explode the string.
Then you can iterate with foreach over the resulting array and calculate them.
$points = "21;48;33;22;31";
$points = explode(";", $points);
$calc = 0;
forearch($points as $point) {
$calc += $point;
}
Or your can use array_sum:
$points = "21;48;33;22;31";
$points = explode(";", $points);
$calc = array_sum($points);
Something like that. Perhaps there are some shorter ways.
$string = "21;48;33;22;31";
$string = explode(";" , "21;48;33;22;31");
$point = 0;
foreach($string as $num)
{ // by using (int) you can convert string to int.
$point = (int)$num + $point;
}
print($point);
// output
155
explode it then loop for sum...
<?php
$total = 0; // for getting the total
$points = "21;48;33;22;31";
$points = explode(';',$points);
for($i=0;$i<count($points);$i++){
$total+= $points[$i];
}
echo $total;
?>
<?php
eval('$sum = ' . str_replace(';','+','21;48;33;22;31').';');
echo $sum;