#1
  1. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,184
    Rep Power
    492

    Unexpected array behaviour


    Hello all, I wonder if someone can uncloud my poor array_fried($brain) ... I have a form which a user enters some wholesale prices, the idea is to allow them to update multiple entries at once. So far, all was working, I'm used to dealing with multiple updates and so on but one thing is starting to drive me insane and that is ...

    PHP Code:
    //    $_POST:-
    //    Array ( 
    //        [rate_peak] => Array ( 
    //            [{5A42CCA9-06BB-1DD7-ABD3-9C3FAD68BB18}] => 0.057 
    //        ) 
    //        [rate_offpeak] => Array ( 
    //            [{5A42CCA9-06BB-1DD7-ABD3-9C3FAD68BB18}] => 0.057 
    //        ) 
    //        [inc_percent] => 50 
    //    )

        
    $imur $_POST['inc_percent'];

        
    // Peak
        
    foreach ($_POST['rate_peak'] as $pk => $pv) {
            
    $mua 0;
            
    $nwp 0;
            
    $mua = ($_POST['rate_peak'][$pk] / 100) * $imur;
            
    $nwp $_POST['rate_peak'][$pk] + $mua;
            
    $update_array[$pk] = array("peak_without_mup" => $_POST['rate_peak'][$pk], "peak_with_mup" => $nwp);
        }

        
    // Offpeak
        
    foreach ($_POST['rate_offpeak'] as $pk => $pv) {
            
    $mua 0;
            
    $nwp 0;
            
    $mua = ($_POST['rate_offpeak'][$pk] / 100) * $imur;
            
    $nwp $_POST['rate_offpeak'][$pk] + $mua;
            
    $update_array[$pk] = array("off_peak_without_mup" => $_POST['rate_offpeak'][$pk], "off_peak_with_mup" => $nwp);
        } 
    I expected an array that looks like this:-

    PHP Code:
    Array (
        [{
    5A42CCA9-06BB-1DD7-ABD3-9C3FAD68BB18}] => Array (
            [
    peak_without_mup] => 0.057
            
    [peak_with_mup] => 0.0855
            
    [off_peak_without_mup] => 0.057
            
    [off_peak_with_mup] => 0.0855
        
    )

    But instead, I get this:-

    PHP Code:
    Array (
        [{
    5A42CCA9-06BB-1DD7-ABD3-9C3FAD68BB18}] => Array (
            [
    off_peak_without_mup] => 0.057
            
    [off_peak_with_mup] => 0.0855
        
    )

    Any ideas why?! I have tried to create two arrays ($peak and $offpeak) then combine them using array_combine and also tried to use $newarray = $peak + $offpeak; to no avail
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984
  2. #2
  3. No Profile Picture
    Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    57
    Rep Power
    0
    Hi,

    Well, currently you're simply overwriting the array. But

    PHP Code:
    $update_array[$pk] += array("off_peak_without_mup" => $_POST['rate_offpeak'][$pk], "off_peak_with_mup" => $nwp); 
    in the second loop should work fine.
  4. #3
  5. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,184
    Rep Power
    492
    Originally Posted by Jacques3
    Hi,

    Well, currently you're simply overwriting the array. But

    PHP Code:
    $update_array[$pk] += array("off_peak_without_mup" => $_POST['rate_offpeak'][$pk], "off_peak_with_mup" => $nwp); 
    in the second loop should work fine.
    Perfect, that's done it, cheers!
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984

IMN logo majestic logo threadwatch logo seochat tools logo