santa
santa

Reputation: 12512

Item position in an array

If I have id number and an array in the following format, how do I find out what count (position number) 'Red' among all other items where cat == 4? The answer I am looking for #3 of 4. Array ( [0] => stdClass Object ( [id] => 1 [cat] => 1 [que] => Description here. )

    [1] => stdClass Object
        (
            [id] => 2
            [cat] => 1
            [que] => Description here.
        )

    [2] => stdClass Object
        (
            [id] => 3
            [cat] => 1
            [que] => Description here.
        )
 ....

    [31] => stdClass Object
        (
            [id] => 32
            [cat] => 4
            [que] => Description here.
        )

    [32] => stdClass Object
        (
            [id] => 33
            [cat] => 4
            [que] => Description here.
        )
    [33] => stdClass Object
        (
            [id] => 34
            [cat] => 4
            [que] => Red.
        )

    [34] => stdClass Object
        (
            [id] => 35
            [cat] => 4
            [que] => Description here.
        )
)

Do I need to split array and then loop? Lost...

--------- EDIT ----------

OK, I did a bad job explaining...

What I meant was: there are many objects withing this array. They are broken into groups, categories. [cat] is the value for each group. There are multiple items in each group. What I was hoping to get was a number of items in each group and the position of particular item withing that group, for example: 4 out of 12.

Upvotes: 0

Views: 215

Answers (3)

Ian Shannon
Ian Shannon

Reputation: 376

$position = 0;
$counts = array();

foreach ($yourarray as $obj) {
    if (!isset($counts[$obj->cat])) $counts[$obj->cat] = 0;
    $counts[$obj->cat]++;
    if ($obj->cat == 4 && $obj->que == "Red") {
        $position = $counts[$obj->cat];
    }
}

if ($position >= 0) {
    echo "Object is $position out of $counts[4] objects in category 4\n";
}
foreach($counts as $key=>$val){
    echo "Category $key has $val objects.\n";
}

(Revised to count all categories)

Upvotes: 2

freejosh
freejosh

Reputation: 11383

My edit to Ian Shannon's answer was rejected, so here's what I was proposing, which builds on his by using object notation instead of array notation and keeps track of the total.

I think the simplest way would be to loop through the array with a foreach, and if cat == 4, increment a counter until you reach the one you want. Something like this:

$pos = 0;
$total = 0;
foreach ($yourarray as $subarray) {
    if ($subarray->cat == 4) {
        $total++;
        if ($subarray->que == "Red") {
            $pos = $total;
        }
    }
}

$pos will be the position of the element you're looking for, and $total will be the total in that category.

Upvotes: 1

martin-lundberg
martin-lundberg

Reputation: 470

$found = 0;
foreach ($array as $key => $value) {
    if($value->cat == '4'){
        $arrayPosition = $key;
        $found++;
    }
}

echo 'Found ' . $found . ' cat\'s, matching the criteria';

Didn't exactly understand what the matching criteria should be except for cat=4, and if "que" contains some more information you want to match, change the if statement.

if($value->cat == '4' && $value->que == 'Red'){

Upvotes: 0

Related Questions