1

I have a set of nested arrays.

[data] is the top level array.

The lower level arrays are all identified by incremental numbers, dynamically assigned...essentially each sub-array identifies a row/record of information.

How would I extract the count of the sub-arrays? I need to be able to do this to post this data into a DB.

The number of sub-arrays is dynamic, and can change depending on what data I am parsing through.

 [data] => Array
        (
            [0] => Array
                (
                    [id] => 3475
                    [name] => Player1
                    [score] => 11870
                    [rank] => 213
                    [total_cities] => 2
                    [crown] => None
                    [alliance_id] => 134
                    [title] => Earl
                    [fame_total] => 509875
                    [fame_rank] => 381
                    [defeated_total] => 3436
                    [defeated_rank] => 376
                    [plunder_total] => 1388754
                    [plunder_rank] => 245
                )

            [1] => Array
                (
                    [id] => 3523
                    [name] => Player2
                    [score] => 1978
                    [rank] => 281
                    [total_cities] => 1
                    [crown] => None
                    [alliance_id] => 134
                    [title] => Baron
                    [fame_total] => 0
                    [fame_rank] => 448
                    [defeated_total] => 0
                    [defeated_rank] => 448
                    [plunder_total] => 0
                    [plunder_rank] => 436
                )

            [2] => Array
                (
                    [id] => 73
                    [name] => Player3
                    [score] => 1308
                    [rank] => 304
                    [total_cities] => 1
                    [crown] => None
                    [alliance_id] => 134
                    [title] => Marquess
                    [fame_total] => 5604153
                    [fame_rank] => 237
                    [defeated_total] => 37270
                    [defeated_rank] => 229
                    [plunder_total] => 68130
                    [plunder_rank] => 335
                )
4
  • I'm not really sure I understand what the question even is, but array_count() with the recursive flag? or simply array_count($myArray['data']) Commented Apr 3, 2013 at 15:21
  • array_count($data) or array_count($_POST['data']) just give me back a no properties flag. What I am trying to simply do is count the number of sub-arrays in a main array. I have figured out what code works below; my next step is to use that code as the loop in which I will run SQL statements to insert/update a database. Commented Apr 3, 2013 at 16:31
  • WHat is a no properties flag? Commented Apr 3, 2013 at 16:54
  • When I try to echo the results of array_count($data) or array_count($_POST['data']) all it shows me in the console is no properties. I have defined @data=$_POST['data'] in my code so either should work. when i try to assign $count = array_count($data) and then do echo $count, all I get in browser console is no properties. that's all it says. of course, that's trying to use the method you highlighted. if you read down I did get my end result, so thanks for responding. Commented Apr 3, 2013 at 18:34

3 Answers 3

2

I used a combination of the methods highlighted by Marc B and Thanos to get my desired result

This is the code I used:

$count = 0;
foreach($data as $data=>$inner_array){
   $count = $count+1;
}

echo $count;

I ran it with a test data set with 143 unique rows, and I got that echo back, so life is good.

Thanks guys.

Sign up to request clarification or add additional context in comments.

Comments

0

Simplest method:

$count = 0;
foreach($mainarray as $subarray)
   $count += count($subarray);
}

Comments

0

Let's assume $data is your main array: This one prints the name of each inner array (0, 1, 2... for your example) and then counts each sub-array items:

echo "Number of sub-arrays: ".count($online_time); //optional
foreach($data as $inner_array=>$data_of_inner_array) {
echo $inner_array;
echo " --- ".count($data_of_inner_array);
echo "<br/>";
}

i assume output will be

Number of sub-arrays: 3
0 --- 14
1 --- 14
2 --- 14

3 Comments

Thanks, but all I am looking to do is count the sub arrays. How would I do that? I don't need to know the element count within the sub array because that is fixed.
isn't it the first row? the "optional"? which is the expected result at your example?
expected result was just a straight count and I have achieved it by the method I have written up above. Thank you for getting me pointed in the right direction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.