简体   繁体   中英

PHP Array Values same element in different formats and posting from div data

I have a foreach loop for an array

 foreach ($somethings as $key2 => $something)
 {
                                    $value = 0;
                                    if ($something['ElementID'] == $value)
                                    {
                                        unset($available);
                                    }
                                    $total += $something['Cost'];
                        $singleprice = $available['Cost'];
}

I need to be able to return $total and $singleprice - Total adding up all the values within the key, $singleprice returning only 1 instead of all added up

The only way I've managed to return this value is by creating another foreach loop within this foreach loop like so:

foreach ($somethings as $key2 => $something)
     {
                                        $value = 0;
                                        if ($something['ElementID'] == $value)
                                        {
                                            unset($available);
                                        }
 foreach($somethings as $key3 => $singlesomething)
                                    {
                            $singleprice = $singlesomething['Cost'];
                                    }
                                        $total += $something['Cost'];
    }

Why will the above first method return nothing? I then use this variable which now has the data in a Div Data- ( data-single-cost="'.$singleprice.'" ) which is then used to POST a form

$singleprice      = $_POST['single-cost'];

Yet it returns 0 even with the second method successfully getting the value

any ideas what I'm doing wrong?

I guess its because somehow program goes into the if and you have unset available in it, so singleprice gots nothing.

Maybe you should get a Xdebug and try some step debug to see what happend.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM