1
votes
Array
(
    [6254] => Array
        (
            [check] => on
            [quantity] => 2
            [name] => Testing product_special One Size
            [total] => 66.0000
            [price] => 33.0000
        )

    [6255] => Array
        (
            [check] => on
            [quantity] => 1
            [name] => Testing card
            [total] => 80.85
            [price] => 33.0000
        )

)

Above is my array .

My Code

foreach($value as $key2 => $value2){
    $name = preg_replace('/\s+/', '', $key);
    echo "<pre>";
    // print_r(array_sum(array_column($value, 'price')));
    print_r($value);
    echo "</pre>";
    exit;
}

wanted to ask , how do i "specifically" add prices ? tried with

$totalprice += $value['price'];

is not working


array_sum(array_column($value, 'price')) < this code fixed my problem


but there is a new question , what if need to check on "condition"

if "check" = on only able to sum , if there is no "check" ignore the array

2
your question is vague? which key are you referring to?Kevin
This array_sum(array_column($value, 'price')) should be working for you.ROOT
@ROOT thanks it work fine , then i have 1 more question . let say in my array have "check" , how to only sum the array if the "check" is on ? got 2 status , "on/off"greenboxgoolu
@greenboxgoolu, glade to help, add your question to the post.ROOT
added my question to the bottomgreenboxgoolu

2 Answers

0
votes

If the array is stored in $value, you should use $totalprice += $value2['price']; since $value2 is an array itself.

0
votes

You need to specify regex pattern in $yourRegex variable.

$total= 0;
foreach($value as $key => $value){
    $yourRegex = '/\s+/';
    if(preg_match($yourRegex, '', $key)) {
        $total+= $value['price'];
    }
}

echo $total;