Social Icons

Pages

Saturday, November 30, 2013

Check Array is multidimension or not - PHP

Most of situation we handle array with php.and data objects.
If you wants to make  a checkup ones array is multi dimension or not ?
Here is the way

$array


Count array twice,once normal count and second time with COUNT_RECURSIVE parameter.and check whether those are equal or not?

If they equal that array is  Single Dimension.
If not that array is             Multi Dimension.
<?php

if (count($myarray) == count($myarray, COUNT_RECURSIVE)) 
{
  echo 'MyArray is not multidimensional';
}
else
{
  echo 'MyArray is multidimensional';
}
          ?>
         

Differences with Count and Recursive Count
<?php
$food 
= array('fruits' => array('orange''banana''apple'),
              
'veggie' => array('carrot''collard''pea'));// recursive count
echo count($foodCOUNT_RECURSIVE); 
      // output 8

      // normal count

      echo count($food);  
      // output 2?>

No comments:

Post a Comment