I Need help on updating Main Array with its data changed in a foreach loop.
Here is my code :
$query = DB::table('autostk')
->where('autostk.branchid', $branch_id)
->where('autostk.itemcode', $request->itemcode)
->whereDate('autostk.date', '<=', $request->tdate)
->where('autostk.branchid', $branch_id)
->leftjoin('journal', 'autostk.refno', '=', 'journal.vno')
->where('journal.code', '>=', 100)
->where('journal.branchid', $branch_id)
->leftjoin('accounts', 'journal.code', '=', 'accounts.code')
->where('accounts.branchid', $branch_id)
->select('journal.code', 'accounts.title', 'autostk.*')
->orderBY('date')->get()
->map(function ($item, $key) {
return (array)$item;
})
->all();
foreach ($query as $row) {
if (is_null($row['qtyin'])) {
$row['qtyin'] = 0;
}
if (is_null($row['qtyout'])) {
$row['qtyout'] = 0;
}
if (is_null($row['rate'])) {
$row['rate'] = 0;
}
if ($row['vtype'] = 'PI' && $row['qtyin'] > 0) {
$stkval = ($bal * $avgrate) + ($row['qtyin'] * $row['rate']);
if ($bal > 0) {
$bal = $bal + $row['qtyin'] - $row['qtyout'];
if ($bal > 0 && $stkval > 0) {
$avgrate = $stkval / $bal;
}
} else {
$bal = $bal + $row['qtyin'] - $row['qtyout'];
$avgrate = $row['rate'];
}
} else {
$bal = $bal + $row['qtyin'] - $row['qtyout'];
}
$row['balqty'] = $bal;
$row['avgrate'] = $avgrate;
}
My question is how to update $query with changes made to $row. I am new to php and laravel and have tried push(), put(), etc. Don't know which function is required in this case.
To keep changes you've applied to $row inside the foreach you just need to pass it by reference:
foreach ($query as &$row) { //notice the & before $row
Alternatively, you could could use just move the code inside your foreach to inside your map closure:
->map(function ($item, $key) use ($bal, $avgrate) {
$row = (array)$item;
if (is_null($row['qtyin'])) {
$row['qtyin'] = 0;
}
if (is_null($row['qtyout'])) {
$row['qtyout'] = 0;
}
if (is_null($row['rate'])) {
$row['rate'] = 0;
}
if ($row['vtype'] = 'PI' && $row['qtyin'] > 0) {
$stkval = ($bal * $avgrate) + ($row['qtyin'] * $row['rate']);
if ($bal > 0) {
$bal = $bal + $row['qtyin'] - $row['qtyout'];
if ($bal > 0 && $stkval > 0) {
$avgrate = $stkval / $bal;
}
} else {
$bal = $bal + $row['qtyin'] - $row['qtyout'];
$avgrate = $row['rate'];
}
} else {
$bal = $bal + $row['qtyin'] - $row['qtyout'];
}
$row['balqty'] = $bal;
$row['avgrate'] = $avgrate;
return $row;
})
Try following code:
$query = DB::table('autostk')
->where('autostk.branchid', $branch_id)
->where('autostk.itemcode', $request->itemcode)
->whereDate('autostk.date', '<=', $request->tdate)
->where('autostk.branchid', $branch_id)
->leftjoin('journal', 'autostk.refno', '=', 'journal.vno')
->where('journal.code', '>=', 100)
->where('journal.branchid', $branch_id)
->leftjoin('accounts', 'journal.code', '=', 'accounts.code')
->where('accounts.branchid', $branch_id)
->select('journal.code', 'accounts.title', 'autostk.*')
->orderBY('date')->get()
->map(function ($item, $key) {
return (array)$item;
})
->all();
$row_data = [];
foreach ($query as $row) {
if (is_null($row['qtyin'])) {
$row['qtyin'] = 0;
}
if (is_null($row['qtyout'])) {
$row['qtyout'] = 0;
}
if (is_null($row['rate'])) {
$row['rate'] = 0;
}
if ($row['vtype'] = 'PI' && $row['qtyin'] > 0) {
$stkval = ($bal * $avgrate) + ($row['qtyin'] * $row['rate']);
if ($bal > 0) {
$bal = $bal + $row['qtyin'] - $row['qtyout'];
if ($bal > 0 && $stkval > 0) {
$avgrate = $stkval / $bal;
}
} else {
$bal = $bal + $row['qtyin'] - $row['qtyout'];
$avgrate = $row['rate'];
}
} else {
$bal = $bal + $row['qtyin'] - $row['qtyout'];
}
$row['balqty'] = $bal;
$row['avgrate'] = $avgrate;
$row_data[] = $row;
}
Now use $row_data.
Option 1
//use toArray() in last to get the result as an array
$query = ...->all()->toArray();
foreach ($query as $key => $row) {
//inside here instead of using $row, use $query[$key]
//so for example $row['rate'] = 0; becomes:
$query[$key]['rate'] = 0;
}
Option 2
//use toArray() in last to get the result as an array
$query = ...->all()->toArray();
//use pass by reference with help of &
foreach ($query as $key => &$row) {
...
}
However, be very careful with a pass by reference approach otherwise you might run into issues if you reuse the same array.
Option 3
$query = ...->all();
foreach ($query as $key => $row) {
//access it as object
//instead of using $row['qtyin'] use:
$row->qtyin = 0;
}
Its dealer's choice :)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Let me start by saying I know this is not the most efficient way of writing this program, but this is the way I've done it, sloppy or not, I just need help understanding why I cannot grab values from my array.
If you run this monstrosity, it will spit out 0s for i and t, my temporary variables. I'm not done setting up all the bug stops, but that is not a my issue. I'm just confused and lost as to why I cannot grab values from my arrays and assign them to new variables. Any input would be GREATLY appreciated. Thank you.
#include <stdio.h>
int main(void)
{
int day1, day2, mnth1, mnth2;
int x, y, i, t;
int jan[31] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
int feb[28] = {32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59};
int mar[31] = {60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90};
int apr[30] = {91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120};
int may[31] = {121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151};
int jun[30] = {152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181};
int jul[31] = {182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212};
int aug[31] = {213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243};
int sep[30] = {244,245,246,247,248,249,250,251,252,253,254,255,256,257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273};
int oct[31] = {274,275,276,277,278,279,280,281,282,283,284,285,286,287,288,289,290,291,292,293,294,295,296,297,298,299,300,301,302,303,304};
int nov[30] = {305,306,307,308,309,310,311,312,313,314,315,316,317,318,319,320,321,322,323,324,325,326,327,328,329,330,331,332,333,334};
int dec[31] = {335,336,337,338,339,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,356,357,358,359,360,361,362,363,364,365};
printf("Please enter the information that is asked only\n");
printf("Please enter the first date: ");
scanf("%d", &mnth1);
scanf("%d", &day1);
printf("%d", mnth1);
printf(" ");
printf("%d\n", day1);
if (mnth1 < 1 | day1 < 1) {
return 0;
}
if (mnth1 == 1) {
if (day1 = jan[i]) {
x = i + 1;
} else if (day1 > 31) {
return 0;
}
} else if (mnth1 == 2) {
if (day1 = feb[i]) {
x = i + 1;
} else if (day1 > 28) {
return 0;
}
} else if (mnth1 == 3) {
if (day1 = mar[i]) {
x = i + 1;
} else if (day1 > 31) {
return 0;
}
} else if (mnth1 == 4) {
if (day1 = apr[i]) {
x = i + 1;
} else if (day1 > 30) {
return 0;
}
} else if (mnth1 == 5) {
if (day1 = may[i]) {
x = i + 1;
} else if (day1 > 31) {
return 0;
}
} else if (mnth1 == 6) {
if (day1 = jun[i]) {
x = i + 1;
} else if (day1 > 30) {
return 0;
}
} else if (mnth1 == 7) {
if (day1 = jul[i]) {
x = i + 1;
} else if (day1 > 31) {
return 0;
}
} else if (mnth1 == 8) {
if (day1 = aug[i]) {
x = i + 1;
} else if (day1 > 31) {
return 0;
}
} else if (mnth1 == 9) {
if (day1 = sep[i]) {
x = i + 1;
} else if (day1 > 30) {
return 0;
}
} else if (mnth1 == 10) {
if (day1 = oct[i]) {
x = i + 1;
} else if (day1 > 31) {
return 0;
}
} else if (mnth1 == 11) {
if (day1 = nov[i]) {
x = i + 1;
} else if (day1 > 30) {
return 0;
}
} else if (mnth1 == 12) {
if (day1 = dec[i]) {
x = i + 1;
} else if (day1 > 31) {
return 0;
}
} else if (mnth1 > 12) {
return 0;
}
printf("Please enter the second date: ");
scanf("%d", &mnth2);
scanf("%d", &day2);
printf("%d", mnth2);
printf(" ");
printf("%d\n", day2);
if (mnth2 == 1) {
if (day2 = jan[t]) {
y = t + 1;
} else if (day2 > 31) {
return 0;
}
} else if (mnth2 == 2) {
if (day2 = feb[t]) {
y = t + 1;
} else if (day2 > 28) {
return 0;
}
} else if (mnth2 == 3) {
if (day2 = mar[t]) {
y = t + 1;
} else if (day2 > 31) {
return 0;
}
} else if (mnth2 == 4) {
if (day2 = apr[t]) {
y = t + 1;
} else if (day2 > 30) {
return 0;
}
} else if (mnth2 == 5) {
if (day2 = may[t]) {
y = t + 1;
} else if (day2 > 31) {
return 0;
}
} else if (mnth2 == 6) {
if (day2 = jun[t]) {
y = t + 1;
} else if (day2 > 30) {
return 0;
}
} else if (mnth2 == 7) {
if (day2 = jul[t]) {
y = t + 1;
} else if (day2 > 31) {
return 0;
}
} else if (mnth2 == 8) {
if (day2 = aug[t]) {
y = t + 1;
} else if (day2 > 31) {
return 0;
}
} else if (mnth2 == 9) {
if (day2 = sep[t]) {
y = t + 1;
} else if (day2 > 30) {
return 0;
}
} else if (mnth2 == 10) {
if (day2 = oct[t]) {
y = t + 1;
} else if (day2 > 31) {
return 0;
}
} else if (mnth2 == 11) {
if (day2 = nov[t]) {
y = t + 1;
} else if (day2 > 30) {
return 0;
}
} else if (mnth2 == 12) {
if (day2 = dec[t]) {
y = t + 1;
} else if (day2 > 31) {
return 0;
}
} else if (mnth2 > 12) {
return 0;
}
printf("The difference between these two dates is:\n");
printf("%d\n", x);
printf("%d\n", y);
// printf("%d\n", y - x);
}
First, many of your secondary if statements are using the assignment operator = and not the comparison ==.
Second, where is i supposed to be set initially? If your goal is for x to be the value from the array, just set it: x = dec[day1]
First time poster, long time reader.
I've been having a problem with figuring this out. I've been stuck in my game for 4 hours now, googling and trying to figure out how to do it.
I have a game where i add some cartoonish ants, that when they are clicked, they need to be removed from stage. There are 4 differend kinds of ants, so im doing a Math.random for picking which one to add. (ant 1+2+3 have 50% chance to spawn and 4th 50%)
rnd_nbr = (Math.random() * 5)+1;
I have a timer doing 10 tick, and i reset the timer to make neverending.
Then i have a math random and if sentences adding mc' to the stage with movement from Tweener, and event listeners for clicks.
But i cant figure out how to remove them when clicked.
I have done alot of failed tries right inside the click_candy_anty function.
I've left them commented out.
I apologize for abit messy coding, but it will be cleaned up when(hopefully) it gets working.
Help is highly appreciated.
import caurina.transitions.Tweener;
var ant_index:Array = new Array(10); //index for ants
var ant_number:int;
var ant_temp:int;
var rnd_nbr:int; //var for rnd number
var score:int = 0;
var score_update:String;
var reset_timer:Boolean = false;
var antTimer:Timer = new Timer(800,10); //timer
antTimer.addEventListener(TimerEvent.TIMER, create_ant);
var anty0_:anty_0; //creating ant vars for all 5 kind
var anty1_:anty_1;
var anty2_:anty_2;
var anty3_:anty_3;
var anty4_:anty_4;
var screen_number:int = 0;
var antyArray:Array = new Array(10);
var main:main_mc = new main_mc;
main.x = 0; //0,0
main.y = 0;
addChild(main);
var score_format:TextFormat = new TextFormat();
score_format.size = 25;
score_format.align = TextFormatAlign.CENTER;
var score_txt:TextField = new TextField();
score_txt.defaultTextFormat = score_format;
score_txt.text = "" + score;
score_txt.x = 600;
score_txt.y = 20;
score_txt.border = true;
score_txt.autoSize = TextFieldAutoSize.LEFT ;
score_txt.height = 40;
addChild(score_txt);
var score_txt_update:TextField = new TextField();
score_txt_update.defaultTextFormat = score_format;
score_txt_update.text = "0";
score_txt_update.x = 550;
score_txt_update.y = 20;
score_txt_update.border = true;
score_txt_update.autoSize = TextFieldAutoSize.LEFT ;
score_txt_update.height = 40;
score_txt_update.alpha = 0;
addChild(score_txt_update);
function click_candy_anty(event:MouseEvent):void{
if (score < 20){
//trace("evt: " + evt);
//this.removeChild();
//this.removeChild(this);
//removeChild(evt.currentTarget);
//removeChild(evt.target.name.substr(7));
//removeChild(this);
//removeChild(evt.currentTarget.name);
// trace("The " + evt.target.label + " button was clicked");
// trace(evt.type)
score++;
score_txt.text = "" + score;
//score_update = "+1";
score_txt_update.text = "+1";
//ori position x:570 y:20
score_txt_update.y = -30;
Tweener.addTween(score_txt_update, {y: 20, alpha: 1, time: 0.8, transition:"linear", onComplete:score_txt_update_fade});
}
else {
stop_game();
trace("48");
}
}
function score_txt_update_fade(Event = null){
Tweener.addTween(score_txt_update, {y: 50, alpha: 0, time: 0.4, transition:"linear"});
}
function click_leaf_anty(Event = null):void{
if (score > 0 && score < 20){
score--;
score_txt.text = "" + score;
score_txt_update.text = "-1";
score_txt_update.y = 50;
Tweener.addTween(score_txt_update, {y: 20, alpha: 1, time: 0.4, transition:"linear", onComplete:score_txt_update_fade_neg});
trace("12 wrong");
}
/* else {
stop_game();
trace("12");
trace("score: " + score + ", ");
}*/
}
function score_txt_update_fade_neg(Event = null){
Tweener.addTween(score_txt_update, {y: -30, alpha: -1, time: 0.8, transition:"linear"});
}
//screen1();
start_timer();
function screen1(Event = null):void {
screen_number = 2;//slet når event listener til scr1 kommer
if (screen_number == 2){
}
else {
screen_number = 2;
}
}
function screen2(Event = null):void {
if (screen_number == 3){
}
else {
screen_number = 3;
}
start_timer();
}
function start_timer(Event = null):void {
if (score < 20) {
if (reset_timer == false){
antTimer.start();
//trace("antTimer initialized");
}
if (reset_timer == true){
antTimer.reset();
antTimer.start();
//trace("antTimer RESETTED & initialized");
}
}
else {
stop_game();
trace("57");
}
}
function stop_game(Event = null):void {
if (screen_number != 2){
screen_number = 2;
trace("Game completed - launching end screen");
// move to next screen
for (var i:Number=0; i<=9;i++){
ant_temp = ant_number;
if (ant_temp < 9) {
//trace("ant_temp er lavere end 9::: " + ant_temp );
ant_temp++;
}
else if (ant_temp == 9) {
//trace("ant_temp lig med 9::: " + ant_temp );
ant_temp = 0;
}
if (ant_index[ant_temp] == 1){
//removeChild(anty1_);
if ("anty1_" + ant_temp != null){
removeChild(getChildByName("anty1_" + (ant_temp)));
}
}
if (ant_index[ant_temp] == 2){
//removeChild(anty2_);
if ("anty2_" + ant_temp != null){
removeChild(getChildByName("anty2_" + (ant_temp)));
}
}
if (ant_index[ant_temp] == 3){
//removeChild(anty3_);
if ("anty3_" + ant_temp != null){
removeChild(getChildByName("anty3_" + (ant_temp)));
}
}
else if (ant_index[ant_temp] == 4){
//removeChild(anty4_);
if ("anty4_" + ant_temp != null){
removeChild(getChildByName("anty4_" + (ant_temp)));
}
}
}//for loop end
screen3();
}
}
function screen3 (Event = null):void{
var end:end_mc = new end_mc;
end.x = 0; //0,0
end.y = 0; //
addChild(end);
}
function create_ant(Event = null):void {
//trace("antTimer triggered");
if (score < 20) {
rnd_nbr = (Math.random() * 5)+1;
ant_index[ant_number] = rnd_nbr;
trace("ant_number" + ant_number);
//trace("ant_index[" + ant_number + "]: " + ant_index[ant_number]);
if (ant_index[ant_number] == 1){
anty1_ = new anty_1();
anty1_.name = "anty1_" + (ant_number);
anty1_.height = 118;
anty1_.width = 102;
anty1_.x = 100;
anty1_.y = 300;
addChild(anty1_);
Tweener.addTween(anty1_, {x: 541, time: 3, transition:"linear"});
anty1_.addEventListener(MouseEvent.MOUSE_DOWN, click_candy_anty);
//trace("anty1_" + ant_number);
//trace("" + anty_1[ant_number].name);
}
if (ant_index[ant_number] == 2){
anty2_ = new anty_2();
anty2_.name = "anty2_" + (ant_number);
anty2_.height = 118;
anty2_.width = 102;
anty2_.x = 100;
anty2_.y = 300;
addChild(anty2_);
Tweener.addTween(anty2_, {x: 541, time: 3, transition:"linear"});
anty2_.addEventListener(MouseEvent.MOUSE_DOWN, click_candy_anty);
//trace("anty2_" + ant_number);
}
if (ant_index[ant_number] == 3){
anty3_ = new anty_3();
anty3_.name = "anty3_" + (ant_number);
anty3_.height = 118;
anty3_.width = 102;
anty3_.x = 100;
anty3_.y = 300;
addChild(anty3_);
Tweener.addTween(anty3_, {x: 541, time: 3, transition:"linear"});
anty3_.addEventListener(MouseEvent.MOUSE_DOWN, click_candy_anty);
//trace("anty3_" + ant_number);
}
else if (ant_index[ant_number] > 3 && ant_index[ant_number] < 7){
anty4_ = new anty_4();
anty4_.name = "anty4_" + (ant_number);
anty4_.height = 118;
anty4_.width = 102;
anty4_.x = 100;
anty4_.y = 300;
addChild(anty4_);
Tweener.addTween(anty4_, {x: 541, time: 3, transition:"linear"});
anty4_.addEventListener(MouseEvent.MOUSE_DOWN, click_leaf_anty);
//trace("anty4_" + ant_number);
}
ant_temp = ant_number;
if (ant_temp < 9) {
//trace("ant_temp er lavere end 9::: " + ant_temp );
ant_temp++;
}
else if (ant_temp == 9) {
//trace("ant_temp lig med 9::: " + ant_temp );
ant_temp = 0;
}
if (ant_index[ant_temp] == 1){
//removeChild(anty1_);
removeChild(getChildByName("anty1_" + (ant_temp)));
}
if (ant_index[ant_temp] == 2){
//removeChild(anty2_);
removeChild(getChildByName("anty2_" + (ant_temp)));
}
if (ant_index[ant_temp] == 3){
//removeChild(anty3_);
removeChild(getChildByName("anty3_" + (ant_temp)));
}
else if (ant_index[ant_temp] == 4){
//removeChild(anty4_);
removeChild(getChildByName("anty4_" + (ant_temp)));
}
/* if (ant_number == 9) { //resets the ant_number - being the end of the 10th ant
ant_number = 0;
start_timer(); // launches the timer again
trace("Timer resetted");
for (var i:Number=0; i<=9;i++){
if (ant_index[i] == 1){
//removeChild(anty1_);
removeChild(getChildByName("anty1_" + (i)));
}
if (ant_index[i] == 2){
//removeChild(anty2_);
removeChild(getChildByName("anty2_" + (i)));
}
if (ant_index[i] == 3){
//removeChild(anty3_);
removeChild(getChildByName("anty3_" + (i)));
}
else if (ant_index[i] == 4){
//removeChild(anty4_);
removeChild(getChildByName("anty4_" + (i)));
}
}//for loop end
}//ends if=9 reset*/
if (ant_number == 9) { //resets the ant_number at 10th ant, and restarts the timer
ant_number = 0;
reset_timer = true;
start_timer();
}
else {
ant_number++;
}
/*else {
ant_number++;
}*/
}
else if (score >= 20 && screen_number != 2){
stop_game();
trace("14");
}
} //create_ant func end
You can also use:
removeChild( evt.currentTarget as DisplayObject );
Or
var clicked_ant:DisplayObject = evt.currentTarget as DisplayObject;
removeChild( clicked_ant );
Let's say that you've created a Ant class, you could have something like this
private function init():void
{
this.addEventListener( MouseEvent.CLICK , clickHandler );
}
private function clickHandler( event:MouseEvent ):void
{
this.removeEventListener( MouseEvent.CLICK , clickHandler );
if( this.parent != null )
this.parent.removeChild( this );
}