0

I get a this kind of a var_dumpwhen put a array inside to it

array(3) 
[0]=> object(AppBundle\Entity\CartBookItem)#196 (5)             
{["bookID":protected]=> int(1) 
["quantity":protected]=> string(2) "10" 
["name":protected]=> string(12) "Harry Potter" 
["price":protected]=> int(700) 
["category":protected]=> string(8) "Children" } 

[1]=> object(AppBundle\Entity\CartBookItem)#184 (5) 
{["bookID":protected]=> int(3) 
["quantity":protected]=> string(1) "6" 
["name":protected]=> string(14) "Harry Potter 2" 
["price":protected]=> int(700) 
["category":protected]=> string(8) "Children" } 

[2]=> object(AppBundle\Entity\CartBookItem)#195 (5) 
{ ["bookID":protected]=> int(2) 
["quantity":protected]=> string(1) "1" 
["name":protected]=> string(9) "the Beast" 
["price":protected]=> int(544) 
["category":protected]=> string(8) "Fiction" } }

[3]=> object(AppBundle\Entity\CartBookItem)#195 (5) 
{ ["bookID":protected]=> int(2) 
["quantity":protected]=> string(1) "7" 
["name":protected]=> string(9) "the Beast 2" 
["price":protected]=> int(544) 
["category":protected]=> string(8) "Fiction" } }

So inside this array what Im trying to do is, to seperately get the quantity of each category. according to this example

Fiction quantity = 7
children quantity = 16

I tried to approach this way but it dint work out

foreach ($bookItems as $key => $bookItem) {
   $q_counts = array_count_values(
      array_column($bookItem, 'category')
   );
}

Can somebody help me how to get the quantity counts separately?

Update getters from cartbookitem

public function getCategory()
    {
        return $this->category;
    }

public function getQuantity()
    {
        return $this->quantity;
    }
5
  • What output does your own function give? I'd be kind of amazed if you could loop through protected properties just like that ;-) Commented Jul 31, 2018 at 11:26
  • no this is inside a symfony project. more code lines are there Commented Jul 31, 2018 at 11:29
  • 2
    OP can you post the getters from CartBookItem. I suspect you have getQuantity() and getCategory() but need confirmation. Commented Jul 31, 2018 at 11:29
  • ^ This what I was alluding to: 3v4l.org/QGBgI Commented Jul 31, 2018 at 11:30
  • Neil Masters.. updated my friend Commented Jul 31, 2018 at 11:32

2 Answers 2

2

You need to iterate over your results and use getCategory() and getQuantity() to produce an array of categories that count up the quantities.

$quantities = [];

foreach ($books as $book) {
    // We need to do an isset check because += on an undefined element
    // will throw an exception.
    //
    // You can remove this with an inventive ?? ternary see
    // https://wiki.php.net/rfc/isset_ternary for example uses.
    if (!isset($quantities[$book->getCategory()])) {
        $quantities[$book->getCategory()] = $book->getQuantity();   
        continue;
    }

    $quantities[$book->getCategory()] += $book->getQuantity();
}

If this code works you should end up with an array similar to:

['Children' => 16, 'Fiction' => 8]

3v4l example

Sign up to request clarification or add additional context in comments.

4 Comments

Please try running this code somewhere before you post it, the last line already has two breaking errors in it. We're here to help people, not to provide them with code that doesn't compile. Idea of the solution is nice however!
Fixed and provided example 3v4l link of working example using OPs example.
@Loek and yet OPs will 99% of the time accept the first answer that provides no elegant solution, up to date syntax nor any explanation. The joys of SO.
The only thing we can do is post better quality answers and hope that some day, in the not too distant future, every programmer will be disciplined enough to write good software. Oh well.
0

you can create a loop like this. untested code but hopefully it will work.

$data=array();
foreach($bookItems as $key => $bookItem){
    if(isset($data[$bookItem->category]) && !empty($data[$bookItem->category])){
        $data[$bookItem->category]=$data[$bookItem->category]+$bookItem->quantity;
    }else{
        $data[$bookItem->category]=$bookItem->quantity;
    }
}

1 Comment

This is not going to work. category and quantity are protected class variables. Without using reflection they are not accessible.

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.