SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist
    Join Date
    Dec 2008
    Location
    Plymouth, United Kingdon
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    count total items in a multidimensional array/ associative array, php

    Hi,

    How can I count the total items in this array below?


    Code:
    Array
    (
        [upload] => Array
            (
                [name] => Array
                    (
                        [0] => 1024x768.jpg
                        [1] => 1280x800.jpg
                        [2] => 1280x1024.jpg
                        [3] => 1440x900.jpg
                    )
    
                [type] => Array
                    (
                        [0] => image/jpeg
                        [1] => image/jpeg
                        [2] => image/jpeg
                        [3] => image/jpeg
                    )
    
                [tmp_name] => Array
                    (
                        [0] => C:\wamp\tmp\php34FE.tmp
                        [1] => C:\wamp\tmp\php353D.tmp
                        [2] => C:\wamp\tmp\php356D.tmp
                        [3] => C:\wamp\tmp\php35AC.tmp
                    )
    
                [error] => Array
                    (
                        [0] => 0
                        [1] => 0
                        [2] => 0
                        [3] => 0
                    )
    
                [size] => Array
                    (
                        [0] => 469159
                        [1] => 602230
                        [2] => 739779
                        [3] => 707039
                    )
    
            )
    
    )

    this is my method, but I think it's stupid! any better ways/ methods to count the total items inside the array?

    PHP Code:
    <pre>
    <?php if ($_FILES) {print_r($_FILES);}?>
    </pre>

    <?php 
    echo count($_FILES['upload']['name']);

    if(empty(
    $_FILES['upload']['name'][0]))
    {
        echo 
    '0 file has been uploaded.';
    }
    ?>
    many thanks,
    Lau

  2. #2
    I am obstructing justice. bronze trophy fatnewt's Avatar
    Join Date
    Jul 2002
    Location
    Ottawa, Canada
    Posts
    1,766
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why do you think your way is stupid?
    Colin Temple [twitter: @cailean]
    Web Analyst at Napkyn


  3. #3
    SitePoint Evangelist
    Join Date
    Dec 2008
    Location
    Plymouth, United Kingdon
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fatnewt View Post
    Why do you think your way is stupid?
    bcos i may want to count other types of multidimensional array at some point, which means i have to change the names below on and off whenever i have a different input..

    for instance,

    PHP Code:
    1. echo count($_FILES['upload']['name']); 

    2. echo count($_FILES['image']['name']); 

    3. echo count($_FILES['video']['name']); 

    4. echo count($_FILES['document']['name']); 

    it's tedious!

  4. #4
    I am obstructing justice. bronze trophy fatnewt's Avatar
    Join Date
    Jul 2002
    Location
    Ottawa, Canada
    Posts
    1,766
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not that tedious... but you could wrap the count call in a function, and have the function take the file type and return the count.

    i.e. echo get_file_count('upload');
    Colin Temple [twitter: @cailean]
    Web Analyst at Napkyn



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •