jeudi 31 décembre 2020

why is my for loop not getting updated value on 2nd iteration?

consider i have an array like this $customersids = [10 , 20 , 30 , 40] out side the loop which i am updating later according to some conditions example if updated array is $customersids = [10 , 30] on second iteration my loop should use this updated $customersids but it is not taking updated and my result is null here is my small piece of code example

        $customersids = Customer::whereIn('created_by', $adminot)->pluck('id')->toArray(); // this id should be updated for looping
                for ($a = 0; $a < 2 ; $a++){
                $customerscords = Customer::whereIn('id', $customersids)->pluck('location_url')->toArray(); **//on first iteration it should use upper $customersids and on 2nd iteration it should use updated $customersids = [10 , 30] but it is not taking updated value**
                foreach ($customerscords as $short){
                 $shortest[] = $this->getsortedDistance($cords , $cords1 ,$short);
                } 
    // example here i updated $customersids = [10 , 30]
}

here is my full code

$adminot = array_merge($ids, User::where('ot_of', Auth::id())->pluck('id')->toArray());
$new_ids = "empty";
$cords = 31.5815886;
$cords1 = 74.3779746;
$customersids = Customer::whereIn('created_by', $adminot)->pluck('id')->toArray(); // this id should be updated for looping
for ($a = 0; $a < 2 ; $a++){
$customerscords = Customer::whereIn('id', $customersids)->pluck('location_url')->toArray();  
foreach ($customerscords as $short){
 $shortest[] = $this->getsortedDistance($cords , $cords1 ,$short);
}
for($i = 0; $i < sizeof($customersids); $i++){
    $maping[$customersids[$i]] =  $shortest[$i];
}
$key_val = array_keys($maping, min($maping));// this gets keys and shortest value
if( $new_ids == "empty"){ // this stores srtoed ids
    $new_ids = $key_val;
}
else{
 array_push($new_ids , $key_val);    
}
$get_last_id = end($new_ids); 
$getcordslastid = Customer::where('id' , $get_last_id)->pluck('location_url')->first();
$getcordslastid = explode(',', $short);
$cords = @$getcordslastid[0];
$cords1 = @$getcordslastid[1];
$customersids = array_diff($customersids , $new_ids); // this sets customer ids for looping
}

why my loop resulting null on second iteration what mistake i am making in it it should use all the updated value on 2nd iteration but it is not taking updated values



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3aWL9Th
via IFTTT

Aucun commentaire:

Enregistrer un commentaire