Page 1 of 1

count total items in a multidimensional array

Posted: Fri Jul 23, 2010 5:43 pm
by lauthiamkok
Hi,

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

Code: Select all

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?

Code: Select all

<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

Re: count total items in a multidimensional array

Posted: Fri Jul 23, 2010 6:51 pm
by yacahuma
please check count function on php.net. There are a bunch of multi-dim array count implementations

Re: count total items in a multidimensional array

Posted: Fri Jul 23, 2010 7:53 pm
by Weirdan
This very much depends on what you think constitute an item there. It could be 4 (if we considered item to be uploaded file), or 20 (if we considered item to be any 'list' item, in other words - non-array), or something completely different. What are you trying to achieve?

Re: count total items in a multidimensional array

Posted: Fri Jul 23, 2010 8:04 pm
by lauthiamkok
Weirdan wrote:This very much depends on what you think constitute an item there. It could be 4 (if we considered item to be uploaded file), or 20 (if we considered item to be any 'list' item, in other words - non-array), or something completely different. What are you trying to achieve?
hi, below is what I am trying to archieve,

Code: Select all

# get size of the $_FILES array.
$size = count($_FILES['image']['name']);
//echo "<error message='$size'/>";

# check if it has more than zero item and it is not empty.
if($size > 0 && !empty($_FILES['image']['name'][0])) 
{
	include('image_upload_qna_xml.php');
}
i want to check if the $_FILES array is not zero and not empty, then start running the uploading script which is in image_upload_qna_xml.php

this is the form,

Code: Select all

<form action="<?php echo HTTP_ROOT.INC_LAYOUT;?>ask_add_xml.php" method="post" enctype="multipart/form-data" id="form_qna_ask">
			
			<div class="item-form">
				<h2><a href="#"><span>ASK</span></a></h2>
				<label id="ask_title_label">
				<input type="text" name="ask_title" id="ask_title" value="" title="TYPE IN A TITLE"/>
				</label>
			</div>
			
			<div class="item-form">
				<h2><a href="#"><span>EMAIL</span></a></h2>
				<label id="ask_email_label">
				<input type="text" name="ask_email" id="ask_email" value="" title="TYPE IN YOUR EMAIL"/>
				</label>
			</div>
			
			<div class="item-form">
				<label id="ask_content_label">
					<textarea name="ask_content" id="content_mce" title="CONTENT"></textarea>
				</label>
			</div>
			
			<div class="item-form">
				<div class="left">
					<label>
					<img src="views/www/styles/images/global/button-add-images-q-n-a.gif" alt="add images" style="float:left;"/> 
					<h2 id="add_images_label"><a href="#"><span>Add Images</span></a></h2>
					</label>
				</div>
				
				<div class="right">
					<div class="processing"></div>
					<input type="submit" value="CANCEL"/>
					<input type="submit" value="SUBMIT"/>
				</div>
			</div>
			
			
			<div class="item-form" style="border:1px solid #bbbbbb; padding:10px 10px 20px 15px; background-color:#ffffff;">
				<p>Add images: this allows you to attach pictures to your question. Only 2 pictures at 2MB each are allowed to upload.</p>
				<input type="file" name="image[]" class="multi" accept="gif|jpg" maxlength="3"/>
			</div>
				
		</form>
does it make any sense!??

Re: count total items in a multidimensional array

Posted: Fri Jul 23, 2010 8:26 pm
by Weirdan
does it make any sense!??
Not much. This is probably what you're looking for:

Code: Select all

if (!$_FILES) { 
   die('nothing was uploaded');
}

if (!isset($_FILES['upload']['name']) || !is_array($_FILES['upload']['name']) {
   die('something was uploaded, but not using the "upload" field');
}

foreach ($_FILES['upload']['name'] as $index => $name) {
    // error codes from http://us2.php.net/manual/en/features.file-upload.errors.php
    switch ($_FILES['upload']['error']) {
        case UPLOAD_ERR_OK:
            // process the file here
            move_uploaded_file($_FILES['upload']['tmp_name'][$index], '/var/data/' . uniqid(basename($name), true));
            break;
        case UPLOAD_ERR_INI_SIZE:
        case UPLOAD_ERR_FORM_SIZE:
            die('file size limit exceeded for ' . $index . ' file');
        case UPLOAD_ERR_PARTIAL:
            die('file ' . $index . ' was uploaded partially');
        case UPLOAD_ERR_NO_FILE:
            die('hmm... file ' . $index . ' was uploaded, but it wasn\'t?');
        case UPLOAD_ERR_NO_TMP_DIR:
            die('we have no upload dir, can\'t accept the file');
        case UPLOAD_ERR_CANT_WRITE:
            die('can\'t write file ' . $index . ' to disk');
        case UPLOAD_ERR_EXTENSION:
            die('some of php extensions stopped the file ' . $index . ' upload');
        default:
            die('unknown error while processing file ' . $index);
    }
}
// all files have been uploaded (and moved to /var/data), continue processing

Re: count total items in a multidimensional array

Posted: Fri Jul 23, 2010 8:42 pm
by lauthiamkok
thanks. it is very close to what in my mind thanks! all i need to change a little to fit everything in! :D