2

I have an array

dump($data);
*************************************

    array(10) {
      ["12-male"] => string(1) "2"
      ["11-male"] => string(1) "2"
      ["10-female"] => string(1) "2"
      ["16-female"] => string(1) "2"
      ["9-male"] => string(1) "2"
      ["17-male"] => string(1) "4"
      ["14-male"] => string(1) "4"
      ["15-female"] => string(1) "4"
      ["13-female"] => string(1) "5"
      ["18-female"] => string(1) "6"
    }

******************************************

I am DYNAMICALLY getting like sub arrays out of the array above

$rooms = array();
foreach ($data as $key => $value) {
    $rooms['room'.$value][] = $key;
        $rooms['room'.$value]['count'] = sizeof($rooms['room'.$value]);
}

        dump($rooms);

******************************************

I get this result

Dump => array(4) {
  ["room2"] => array(6) {             //array size=6
    [0] => string(7) "12-male"
    ["count"] => int(6)               //count of array size=6
    [1] => string(7) "11-male"
    [2] => string(9) "10-female"
    [3] => string(9) "16-female"
    [4] => string(6) "9-male"
  }
  ["room4"] => array(4) {             //array size=4
    [0] => string(7) "17-male"
    ["count"] => int(4)               //count of array size=4
    [1] => string(7) "14-male"
    [2] => string(9) "15-female"
  }
  ["room5"] => array(2) {             //array size=2
    [0] => string(9) "13-female"
    ["count"] => int(1)               //count of array size=1 (the problem here)
  }
  ["room6"] => array(2) {             //array size=2
    [0] => string(9) "18-female"
    ["count"] => int(1)               //count of array size=1 (the problem here)
  }
}

My issue is that, the count is returned correctly after first 2 iterations, after that the count is always showing 1, no matter the size of array. I tried count() as well but the result is the same.

0

2 Answers 2

5

You could do like below:

$rooms = array();
foreach ($data as $key => $value) {
    if (!isset($rooms['room'.$value])) {
         $rooms['room'.$value] = array('count' => 0);
    }
    $rooms['room'.$value][] = $key;
    $rooms['room'.$value]['count']++;
}

But you don't need to add the count into your array.

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

4 Comments

And what if i want to keep the count in the same array?
@user3269869 Does the answer give you the result you expected?
yes that helped me a lot, thanks .... but can you tell me how can i insert count in my created array ?
@user3269869 The code already did that, not? Although I do not recommend that.
3

The reason the count is doing that is that from room2 and room4 you are inserting 'count' on the first iteration, then on subsequent iterations 'count' is included in the sizeof() request. For room 5 and room6 as they are iterated only once sizeof() is only called once, before 'count' is inserted into the array, so it's not the index of 'count' not included in the result of sizeof for those items.

Comments

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.