0

How can I count the number of items with the same folder_id# ?

Here's my list of items:

  1. item_id=1, folder_id=1
  2. item_id=2, folder_id=1
  3. item_id=3, folder_id=2
  4. item_id=4, folder_id=3

Here's my UPDATED code:

foreach($items as $item)
{
    if(????) //count of $item->folder_id > 1
       {
         //do something to $item->folder_id=1/$item->item_id=1
       }

       elseif(????) // cases where $item->item_id != $item->folder_id
       { 
         //do something else to $item->folder_id=1/$item->item_id=2
       }

       else
       { 
         //do something else to $item->folder_id=2/$item->item_id=3 and folder_id=3/item_id=4
       }
}

I'm interested in code that can tell me that the count for folder_id=1 is 2, the count for folder_id=2 is 1, and the count for folder_id=3 is also 1.

UPDATE: I've changed the code above to now include an elseif() because it didn't quite ask all the things I was interested in. Besides counting the # of each folder_id, I'm also interested in distinguishing cases where folder_id != item_id. This would put item_id=1, item_id=2, item_id=3/4 in their own conditional clauses and wouldn't lump item_id=1 and item_id=2 as before.

Any assistance would be greatly appreciated, thank you,

4 Answers 4

2

If you had an array of folder_ids, you could use array_count_values to provide exactly the result you need. So let's make an array like that out of an array of objects using array_map:

$callback = function($item) { return $item->folder_id; };
$result = array_count_values(array_map($callback, $items));

This will end up with $result being

array(
    1 => 2,
    2 => 1,
    3 => 1,
);

You can also write the callback inline for an one-liner, or if you are on PHP < 5.3 you can write it as a free function or alternatively using create_function.

See it in action (version for PHP < 5.3).

Update: follow up

Given $results from above, your loop would be:

foreach($results as $folder_id => $count) {
    if($count > 1) {
        // There were $count items with folder_id == $folder_id
    }
    // else blah blah
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Jon, thanks for your help, can you follow up by helping me figure out how to put $result into the if() statement I'm interested in? thanks! -tim
Hi Jon, see my comment above which i will repeat here: can i add one more wrinkle? how could I distinguish between item_id=1 and item_id=2? As it stands, both would be placed in the if() statement. What if I wanted item_id=1 in the if() statement, item_id=2 in an elseif(), and item_id=3 and item_id=4 in the else{} statement. Including an && $item->folder_id==$item->item_id along with the $totals[$item... in the if() statement isn't working for me. thanks! -tim
0
$totals = array();

foreach($items as $item) {
   $totals[$item->folder_id]++;
}

6 Comments

Very elegant way to do it but you could explain what it is that your code does.
@Iznogood It just iterates over the $items, adding one to each member of the same name (which is $item->folder_id).
hi Alex, i'm confused on how to use $totals for each folder_id. Meaning, can I put this in the if() statement where $totals>1? thanks for your help! -tim
@alex I knew what it did but did not expect everyone to understand.
@timpeterson just have another foreach: foreach($items as $item) { if ($totals[$item->folder_id]>1) {do_wathever; ...} else { do_other_things; ...}} }
|
0
function count_items(Array $items, $folder_id) {
    return count(array_filter($items, 
        function($item) use($folder_id) {
            return $item->folder_id === $folder_id;
        }
     ));
}

Comments

0

I think this is what you are looking for.

function folder_count($items, $num)
{
    $cnt = 0;
    foreach($items as $item)
    {
        if($item->folder_id > $num)
        {
            $cnt++;
        }
    }
    return $cnt;
}

foreach ($items as $item)
{
    if (folder_count($items, 1))
    {
        //do something to $item->folder_id=1/$item->item_id=1, item_id=2
    }
    else
    { 
        //do something else to $item->folder_id=2/$item->item_id=3 and folder_id=3/item_id=4
    }
}

5 Comments

Where is $items from in your function? This code will not work.
$items is my object which contains the item_id and folder_id properties
I thought it was a global variable, but I fixed the code to work fully as a function.
hi Matt, thanks for your help, can you follow up with how to integrate folder_count() into the if() statement i'm interested in? thanks! -tim
Thank you for fixing your answer.

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.