Array Indexes?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Array Indexes?

Post by spacebiscuit »

Hi guys - is there a function to find the total number of indexes in a nth doimnsion of an array? the count() function seems to work for the first dimension but I can't find anything to find teh second?

Thanks,

Rob.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Array Indexes?

Post by Jonah Bron »

None that I know of, but it should be easy to make one.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Array Indexes?

Post by Weirdan »

PHP does not have multidimensional arrays (neither has C). It uses arrays of arrays instead - and as such the contained arrays ("rows" in case of 2d array) could be of varying dimensions:

Code: Select all

$a = array( // sizeof == 2
  array(1,2,3), // sizeof == 3
  array(1,2,3,4,5,6,7), //sizeof == 7
);
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Array Indexes?

Post by Jonah Bron »

I think he's looking to count all indices of sub arrays, like this:

Code: Select all

function countDimension($array, $n){}

$array = new array( // sizeof == 4
    array(              // sizeof == 3
        1,
        2,
        array(              // sizeof == 2
            5,
            8
        )
    ),
    94,
    435,
    array(              // sizeof == 2
        8,
        2
    )
);

echo countDimension($array, 0); // 4 (4)
echo countDimension($array, 1); // 5 (2+3)
echo countDimension($array, 2); // 2 (2)
echo countDimension($array, 3); // 0 (no such level)
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Array Indexes?

Post by spacebiscuit »

Yes I am looking for the sub indicies of the sub array. I need the total as a variable in my for loop, unless there is a while loop I could use in this case?

Rob.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Array Indexes?

Post by Jonah Bron »

A for loop? Maybe you had better post your code so we can see what you're trying to accomplish. You don't always know what you want :)
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Array Indexes?

Post by AbraCadaver »

Probably foreach() as it will loop over each element and you can test for an array and foreach() that or just get the count:

Code: Select all

$a = array( // sizeof == 2
  array(1,2,3), // sizeof == 3
  array(1,2,3,4,5,6,7), //sizeof == 7
);

foreach($a as $child) {
   if(is_array($child)) {
      echo count($child);
   }
}
If you have multiple nesting then a recursive function would do it.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply