youngstorm Posted September 17, 2012 Share Posted September 17, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/268455-testing-the-first-dimension-of-an-array-for-arrays/ Share on other sites More sharing options...
requinix Posted September 17, 2012 Share Posted September 17, 2012 I'm not sure what you're trying to do, and the code you have doesn't help at all (why you're using an object, what that each() in arrayCount() is supposed to be for, why you use func_get_arg(0) in the constructor when you could just use a normal parameter...), so I'll just throw out $counts = array_map("count", $a1); Quote Link to comment https://forums.phpfreaks.com/topic/268455-testing-the-first-dimension-of-an-array-for-arrays/#findComment-1378522 Share on other sites More sharing options...
vivekanand25 Posted September 17, 2012 Share Posted September 17, 2012 <?php $arr=array( array(1,2,3,4,5,6,7,8,9), array(1,2,3,4,5,6), array(1,2,3,4,5,6,7, ); echo "<pre>"; print_r($arr); echo "</pre>"; $count=count($arr); echo "There are $count Array"; echo "<br/>"; for($i=0;$i<$count;$i++){ echo ($i+1)."have ".count($arr[$i]); echo "<br/>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/268455-testing-the-first-dimension-of-an-array-for-arrays/#findComment-1378533 Share on other sites More sharing options...
Barand Posted September 17, 2012 Share Posted September 17, 2012 But what if $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, ); Quote Link to comment https://forums.phpfreaks.com/topic/268455-testing-the-first-dimension-of-an-array-for-arrays/#findComment-1378554 Share on other sites More sharing options...
Barand Posted September 17, 2012 Share Posted September 17, 2012 How about function count_arrays($arr) { return substr_count(print_r($arr,1), 'Array'); } Quote Link to comment https://forums.phpfreaks.com/topic/268455-testing-the-first-dimension-of-an-array-for-arrays/#findComment-1378581 Share on other sites More sharing options...
requinix Posted September 17, 2012 Share Posted September 17, 2012 How about function count_arrays($arr) { return substr_count(print_r($arr,1), 'Array'); } count_arrays(array("Array")); So I guess we're counting how many items are arrays? return count(array_filter($arr, "is_array")); Quote Link to comment https://forums.phpfreaks.com/topic/268455-testing-the-first-dimension-of-an-array-for-arrays/#findComment-1378680 Share on other sites More sharing options...
youngstorm Posted September 17, 2012 Author Share Posted September 17, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/268455-testing-the-first-dimension-of-an-array-for-arrays/#findComment-1378742 Share on other sites More sharing options...
requinix Posted September 17, 2012 Share Posted September 17, 2012 Im not sure how to use count_arrays(array("Array")); though. I can play with it tonight. I was just pointing out the one issue with Barand's code that may or may not be an issue for you: it actually looks for the word "Array" in the output. But if your arrays consist of just numbers (and arrays) then it may be quite an effective, if unusual, solution. Quote Link to comment https://forums.phpfreaks.com/topic/268455-testing-the-first-dimension-of-an-array-for-arrays/#findComment-1378744 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.