Webiny\Component\StdLib\StdObject\ArrayObject\ArrayObject::countValues PHP Method

countValues() public method

NOTE: This function can only count array values that are type of STRING of INTEGER.
public countValues ( ) : ArrayObject
return ArrayObject An ArrayObject containing the array values as keys and number of their occurrences as values.
    public function countValues()
    {
        try {
            /**
             * We must mute errors in this function because it throws a E_WARNING message if array contains something
             * else than STRING or INTEGER.
             */
            @($result = array_count_values($this->val()));
            return new ArrayObject($result);
        } catch (\ErrorException $e) {
            throw new ArrayObjectException(ArrayObjectException::MSG_INVALID_COUNT_VALUES);
        }
    }

Usage Example

Example #1
0
 /**
  * @dataProvider arraySet2
  */
 public function testCountValues($array)
 {
     $a = new ArrayObject($array);
     $valueCount = $a->countValues();
     $this->assertSame(array_count_values($array), $valueCount->val());
 }