1

I have an array with the following structure:

    Array
    (
        [DigitalAssets] => Array
    (
        [0] => Array
            (
                [PartNumber] => 0276S-4
                [Link] => https://1ddf4b1b856a39e33863-d785dc0e3b62b5e0ef07f55db00b0659.ssl.cf2.rackcdn.com/Holley/0576s-4.jpg
                [AssetTypeCode] => P04
                [FileName] => 0576s-4.jpg
                [RecordModifiedDate] => 2020-05-13T18:59:10.28
            )

        [1] => Array
            (
                [PartNumber] => 0437S-4
                [Link] => https://1ddf4b1b856a39e33863-d785dc0e3b62b5e0ef07f55db00b0659.ssl.cf2.rackcdn.com/Holley/0437s-4.jpg
                [AssetTypeCode] => P04
                [FileName] => 0437s-4.jpg
                [RecordModifiedDate] => 2020-05-13T18:59:11.687
            )

        [2] => Array
            (
                [PartNumber] => 0574S-4
                [Link] => https://1ddf4b1b856a39e33863-d785dc0e3b62b5e0ef07f55db00b0659.ssl.cf2.rackcdn.com/Holley/0574s-4.jpg
                [AssetTypeCode] => P04
                [FileName] => 0574s-4.jpg
                [RecordModifiedDate] => 2020-05-13T18:59:12.593
            )

I want to count the total Indexes of array so that I will run loop accordingly. I used Count($array) and Count ($array,RECURSIVE) but it cannot return the correct total number of indexes.

Can only guide that how to do this?

Thanks

5
  • 2
    What exactly do you want to count? The number of nested elements, the number of elements in DigitalAssets? You also could use foreach() instead. Commented May 18, 2020 at 12:20
  • 1
    You have an array nested into an array here. Your outer level only has one entry, that under the key DigitalAssets. And the array under that key than in turn has multiple entries, which you can count with a simple count() … you just need to move to the correct level first. Commented May 18, 2020 at 12:24
  • Yes I want to count the total index numbers in DigitalAssets Array. Commented May 18, 2020 at 13:40
  • @cbroe: Can you provide an example? Commented May 18, 2020 at 13:41
  • What, the mere counting? That has already been answered. And if you just want to loop over all times, then you don’t necessarily need to count them first, you can also use a foreach loop, as Nigel already said. (All in all, this is stuff you should be learning from a beginner tutorial, rather than ask about here.) Commented May 18, 2020 at 13:44

1 Answer 1

2

If You want to count DigitalAssets direct children You can do this count($array['DigitalAssets'])

Here are some tests how count should work.

Code is:

$test = array
        (
            'DigitalAssets' => array
            (
                0 => array(
                    'PartNumber' => '0276S-4',
                    'Link' => 'https://1ddf4b1b856a39e33863-d785dc0e3b62b5e0ef07f55db00b0659.ssl.cf2.rackcdn.com/Holley/0576s-4.jpg',
                    'AssetTypeCode' => 'P04',
                    'FileName' => '0576s-4.jpg',
                    'RecordModifiedDate' => '2020-05-13T18:59:10.28'
                ),
                1 => array(
                    'PartNumber' => '0437S-4',
                    'Link' => 'https://1ddf4b1b856a39e33863-d785dc0e3b62b5e0ef07f55db00b0659.ssl.cf2.rackcdn.com/Holley/0437s-4.jpg',
                    'AssetTypeCode' => 'P04',
                    'FileName' => '0437s-4.jpg',
                    'RecordModifiedDate' => '2020-05-13T18:59:11.687'
                ),
                2 => array
                (
                    'PartNumber' => '0574S-4',
                    'Link' => 'https://1ddf4b1b856a39e33863-d785dc0e3b62b5e0ef07f55db00b0659.ssl.cf2.rackcdn.com/Holley/0574s-4.jpg',
                    'AssetTypeCode' => 'P04',
                    'FileName' => '0574s-4.jpg',
                    'RecordModifiedDate' => '2020-05-13T18:59:12.593'
                )
            )
        );

        echo count($test['DigitalAssets'])." ".count($test['DigitalAssets'], 0)." ".count($test['DigitalAssets'], 1);
        exit();

Result in my case: 3 3 18

This means taht in my case mode in count is set to 0 by default so i will get only first level counted. If i set mode to 1 i will get all nested items counted as well. This should clear things up for You.

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

5 Comments

i think he is talking about foreach($array['DigitalAssets'] as $value) { $indexes += count($value); }
@Milan Rilex Ristic: When I echo $indexes it gives empty result. I just want to echo the count of total indexes in an array.
define it first like $indexes=array(); in parent scope
@Milan Rilex Ristic: with your code in first comment, I receive result 4025 because array has total 804 indexes. And each index has 5 values as I mentioned in my code. I need result 804 to be displayed instead of 4025
Please try with seccond option set to 0 in count function use, from php manual: If the optional mode parameter is set to COUNT_RECURSIVE (or 1), count() will recursively count the array. This is particularly useful for counting all the elements of a multidimensional array.

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.