Counts the dimensions of an array. If $all is set to false (which is the default) it will
only consider the dimension of the first element in the array.
public static countDim ( array $array, boolean $all = false, integer $count ) : integer | ||
$array | array | Array to count dimensions on |
$all | boolean | Set to true to count the dimension considering all elements in array |
$count | integer | Start the dimension count at this number |
Результат | integer | The number of dimensions in $array |
/** * TestCountDim method * * @return void */ public function testCountDim() { $data = ['one', '2', 'three']; $result = Utility::countDim($data); $this->assertEquals(1, $result); $data = ['1' => '1.1', '2', '3']; $result = Utility::countDim($data); $this->assertEquals(1, $result); $data = ['1' => ['1.1' => '1.1.1'], '2', '3' => ['3.1' => '3.1.1']]; $result = Utility::countDim($data); $this->assertEquals(2, $result); $data = ['1' => '1.1', '2', '3' => ['3.1' => '3.1.1']]; $result = Utility::countDim($data); $this->assertEquals(1, $result); $data = ['1' => '1.1', '2', '3' => ['3.1' => '3.1.1']]; $result = Utility::countDim($data, true); $this->assertEquals(2, $result); $data = ['1' => ['1.1' => '1.1.1'], '2', '3' => ['3.1' => ['3.1.1' => '3.1.1.1']]]; $result = Utility::countDim($data); $this->assertEquals(2, $result); $data = ['1' => ['1.1' => '1.1.1'], '2', '3' => ['3.1' => ['3.1.1' => '3.1.1.1']]]; $result = Utility::countDim($data, true); $this->assertEquals(3, $result); $data = ['1' => ['1.1' => '1.1.1'], ['2' => ['2.1' => ['2.1.1' => '2.1.1.1']]], '3' => ['3.1' => ['3.1.1' => '3.1.1.1']]]; $result = Utility::countDim($data, true); $this->assertEquals(4, $result); $data = ['1' => ['1.1' => '1.1.1'], ['2' => ['2.1' => ['2.1.1' => ['2.1.1.1']]]], '3' => ['3.1' => ['3.1.1' => '3.1.1.1']]]; $result = Utility::countDim($data, true); $this->assertEquals(5, $result); $data = ['1' => ['1.1' => '1.1.1'], ['2' => ['2.1' => ['2.1.1' => ['2.1.1.1' => '2.1.1.1.1']]]], '3' => ['3.1' => ['3.1.1' => '3.1.1.1']]]; $result = Utility::countDim($data, true); $this->assertEquals(5, $result); $set = ['1' => ['1.1' => '1.1.1'], ['2' => ['2.1' => ['2.1.1' => ['2.1.1.1' => '2.1.1.1.1']]]], '3' => ['3.1' => ['3.1.1' => '3.1.1.1']]]; $result = Utility::countDim($set, false, 0); $this->assertEquals(2, $result); $result = Utility::countDim($set, true); $this->assertEquals(5, $result); $data = ['one' => [null], ['null' => null], 'three' => [true, false, null]]; $result = Utility::countDim($data, true); $this->assertEquals(2, $result); }