0

I have a cart session with below elements:

print_r($_SESSION['cart']);

Array
(
    [0] => Array
        (
            [p_name] => X-Dot Motorbike Helmet G88 + Bogo Visor (Test)
            [p_code] => 2102649
            [p_id] => 12332
            [p_price] =>  1.60
            [p_seller] => 230002
            [p_alt-variation-1] => Red
            [p_alt-variation-2] => L - 1.60-36
            [p_qty] => 1
        )

    [1] => Array
        (
            [p_name] => Salt and Lemon Candy (20 Packs/Carton)
            [p_code] => 3443268
            [p_id] => 11654
            [p_price] => 1.20
            [p_seller] => 230002
            [p_qty] => 1
        )

    [2] => Array
        (
            [p_name] => Romoss Rolink Hybrid Premium Cable
            [p_code] => TEST421
            [p_id] => 10670
            [p_price] => 13.90
            [p_seller] => 230001
            [p_qty] => 1
        )

)

As seen on the [p_seller], I want to group the same p_seller value and return the number :

return 2 through the array (230002), and return 1 through the array (230001)

how can I do that?

3
  • p_seller has **230001** value or you trying to bold those numbers ??? Commented Dec 23, 2016 at 6:56
  • bold only, removed Commented Dec 23, 2016 at 6:58
  • the number is not total with same element Commented Dec 23, 2016 at 7:03

3 Answers 3

1

Maybe You have to try this code

<?php

$array = array(
    array(
        'p_name' => "X-Dot Motorbike Helmet G88 + Bogo Visor (Test)",
        'p_seller' => '230002',
        'last_name' => 'Doe',
    ),
    array(
        'p_name' => "X-Dot Motorbike Helmet G88 + Bogo Visor (Test)",
        'p_seller' => '230002',
        'last_name' => 'Doe',
    ),
    array(
        'p_name' => "X-Dot Motorbike Helmet G88 + Bogo Visor (Test)",
        'p_seller' => '230001',
        'last_name' => 'Doe',
    )
);

$result = array_count_values(array_column($array, 'p_seller'));
   arsort($result);// not neccesary
   echo "<pre>";print_r($result);exit;
?>

OUTPUT

Array
(
    [230002] => 2
    [230001] => 1
)
Sign up to request clarification or add additional context in comments.

Comments

0
foreach ($_SESSION['cart'] as $Arrvalues){
  $total+= $Arrvalues['p_seller'];
}
echo $total;

output : 690005

Comments

0

If you know the array indexeswill always same, then you can simple sum the values.

Try

echo $_SESSION['cart'][1]['p_seller'] + $_SESSION['cart'][2]['p_seller'];

For Dynamically

You can use array_sum with foreach loop

Try

foreach ($_SESSION['cart'] as $val)
{
    if( isset($val['p_seller']) )
        $total[] = $val['p_seller'];
}
if( isset($total) && is_array($total) )
{
    $total = array_sum($total);
    echo $total;
}

Update 1

As I understand you just want to echo the values found in INDEX p_seller.

Try

$i = 0;
foreach ($_SESSION['cart'] as $val)
{
    if( isset($val['p_seller']) )
    {
        echo ++$i.' - '.$val['p_seller']."<br >";
    }
}

2 Comments

Sorry, maybe my question is not clear enough, i want to return the number found with same ID, instead of sum up the ID. From the above example, I expect 2 and 1 as the results.
I try your update answer, get stuck on $++i, error Parse error: syntax error, unexpected '++' (T_INC), expecting variable (T_VARIABLE) or '$' in C:\xampp...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.