Jump to content

Recommended Posts

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

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);

<?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,8)

);

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/>";

}

?>

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,
);

How about

 

function count_arrays($arr)
{
    return substr_count(print_r($arr,1), 'Array');
}

 

:P

count_arrays(array("Array"));

 

So I guess we're counting how many items are arrays?

return count(array_filter($arr, "is_array"));

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.

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.