Jump to content

youngstorm

New Members
  • Posts

    2
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

youngstorm's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. First, thank you so much to everyone who responded. Second, Let me clarify (I posted my question when I should have been in bed, sorry). I want to count the number of arrays in the second dimension of a multidimensional array. I'd like to be able to detect a 3 or more dimensional array and return an error also for cool error handling, but I'm not quiet there yet. The reason for going thru a construct and using func_get_arg is to count the number of args passed in and if the user passes in several single dimensional arrays I can make them into a multidimensional array first. I want, in the end, 1 array, containing 2 or more, 1 dimensional arrays. vivekanand25: I have not had a chance to try your code yet but I will later this afternoon. Barand: Nice observation with your array, this at least would return "4" as there are 4 arrays in the 2nd dimension or, ideally, return an error stating that the array is more than 2 dimensional. $arr=array( array(1,2,3,4,5,6,7,8,9), array(1,2,3,4,5,6), 'xyz', array ( array(1,2), 'abc', array(3,4) ), array(1,2,3,4,5,6,7, ); Also, your "substr_count(print_r($arr,1), 'Array');" code works well. Of course, I have to assume I have a 2D array already but it does good for now. I just substract 1 from the results and Im good to go. requinix: I experimented with $counts = array_map("count", $a1); for a while and was not able to make it work for my purpose but it was still a good experience for meas I did not know about array_map(). Your return count(array_filter($arr, "is_array")); works great. Im not sure how to use count_arrays(array("Array")); though. I can play with it tonight. Thank you to everyone again.
  2. Hi, I am try to count the number of arrays in an array. Here is an example. ######################### class arrayCount { private $arrays = array(); public function __construct() { $arrays = func_get_arg(0); $this->arrayCount($arrays); } private function arrayCount($a) { for ($count=0 ; is_array($a[$count]) ; $count++) { each($a[$count])!=false; } print("Number of arrays: " . $count . "\n"); } } $a1 = array( array(1,3,2,4,5,6,7,8,9,10), array(2,4,6,8,10,12,14), array(1,2,3,4,5,6,14,15,16,17), ); $obj1 = new arrayCount($a1); #################################### The following code is ran by "php test.php" and outputs the following. PHP Notice: Undefined offset: 3 in <filename> Number of arrays: 3 Changing the arrayCount function to the following does and running it gives. PHP Warning: Variable passed to each() is not an array or object in <filename> Number of arrays: 3 ################################## private function arrayCount($a) { for ($count=0 ; each($a[$count])!=false ; $count++); print("Number of arrays: " . $count . "\n"); } ##################################### How can I do this? Thank you
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.