0

I have a multi-dimensional array which consists of indexed arrays within an associative array.

How can I get a separate count of indexed arrays as well as associative arrays which host the indexed array.

Using sizeof($jArray, 1) results into 197 in my case which is the total of key-value pairs + indexed arrays + associative array (49*3 + 49 + 1).

Count($jArray) results into 1, which I'm guessing is the associative array.

JSON structure:

Name
│
│───[0] 
│    └── Key -> Value
│    └── Key -> Value
│    └── Key -> Value
└───[1] 
│    └── Key -> Value
│    └── Key -> Value
│    └── Key -> Value
│...
│
└───[n]
     └── Key -> Value
     └── Key -> Value
     └── Key -> Value

The count(s) that I'm looking for here are

49 - (indexed arrays within associative array 'Name')
147 - (total key-value pairs within indexed array)
3 - (key-value pairs within an indexed array)
3
  • 1
    That count looks funny... any chance that's actually an object and not an array? Commented Sep 16, 2018 at 15:45
  • 1
    Any chance you can include a part of the array as Json or var_export for us to try on? Commented Sep 16, 2018 at 15:49
  • @Andreas, Sorry I was unable to include the json. I've managed to get the count now. Thank you for your help. Commented Sep 18, 2018 at 7:24

2 Answers 2

1

The following line of code reproduce your multidimensional array for test purpose:

 $arrs=array_fill_keys(['name'],array_fill(0,49,['key1'=>1,'key2'=>2,'key3'=>2,]));

According to this:

49 - (indexed arrays within associative array 'Name')

147 - (total key-value pairs within indexed array)

3 - (key-value pairs within an indexed array)

and specifically the third line,all your counts are simplified to some simple operations that you can achieve this way:

$InArWiAsArNa=count($arrs['name']);
$KV_wi_IA=count(current($arrs['name']));
$total_KV_pairs_wi_IA=$InArWiAsArNa*$KV_wi_IA;

var_dump($InArWiAsArNa,$total_KV_pairs_wi_IA,$KV_wi_IA);

output:

int(49)
int(147)
int(3)

however if you remove the third condition, for example if the key-value pairs within an indexed array number is variable, this mean you don't need it anymore so you should loop through the array to get the two first answers:

$InArWiAsArNa=0;
$total_KV_pairs_wi_IA=0;
foreach($arrs['name'] as $key=>$val){
    $InArWiAsArNa ++;
    $total_KV_pairs_wi_IA+=count($val);  
}

var_dump($InArWiAsArNa,$total_KV_pairs_wi_IA);

output:

 int(49)
 int(147)
Sign up to request clarification or add additional context in comments.

1 Comment

Very detailed and informative. Thank you. That worked :)
1

you can always use count() to do that, for example if you have the following array:

 $arrs = [
'name1' => [
    [
        'key1'=>1,
        'key2'=>2,
    ],
    [
        'key3'=>3,
    ],
    [
        'key4'=>4,
        'key5'=>5,
        'key6'=>6,
    ],
],
'name2' => [
    [
        'key7'=>7,
    ],
],

];

$arrs is a multidimensional array of order 3, to get the number of elements in level 0 which are name1 and name2 (associative arrays),you can just use : count($arrs);, this will return 2. if you want the number of elements of name1 : count($arrs[name1]), this will return 3. if you want the total of elements in name1 and name2,

$total=0; foreach($arrs as $arr) $total+=count($arr); if you want the total of key-value pairs :

foreach($arrs as $arr){

foreach($arr as $ar) $total+=count($ar); }

Comments

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.