Pathik Vejani
Pathik Vejani

Reputation: 4491

count array in PHP giving unexpected output

I have 2 following data:

$temp = Array
(
    [@url] => url
    [@type] => image/jpeg
    [@expression] => full
    [@width] => 644
    [@height] => 429
)

$count_total = count($temp); // gives me 5, how can it give me total = 1?

Array
(
    [0] => Array
        (
            [@url] => url1
            [@type] => image/jpeg
            [@expression] => full
            [@width] => 800
            [@height] => 621
        )

    [1] => Array
        (
            [@url] => url2
            [@type] => application/x-shockwave-flash
        )

)
this is total:2 // this is correct

How can I get first array count as 1?

Upvotes: 0

Views: 70

Answers (6)

Iain
Iain

Reputation: 387

It is counting the amount of elements in the array. The first is giving you 5 because the array has 5 elements. The second is giving you 2 because it is a multi-array containting 2 elements (arrays).

To make it return one you can put the array inside of an array, so like this:

Array([0] => Array())

I hope this answered your question.

Upvotes: 0

pesoklp13
pesoklp13

Reputation: 349

$temp = Array 
       (
       [0] => Array
              (
                   [@url] => url
                   [@type] => image/jpeg
                   [@expression] => full
                   [@width] => 644
                   [@height] => 429
               )
       );

$count_total = count($temp); //gives you 1

Problem is that in your first block of code you are trying to count number of elements of array which in second block is insido of another array.

Upvotes: 0

Maths RkBala
Maths RkBala

Reputation: 2195

If you got single result, you do the following:

$temp = Array
(
    [@url] => url
    [@type] => image/jpeg
    [@expression] => full
    [@width] => 644
    [@height] => 429
);

Single result:

$new_temp[0] = $temp;

Now you get:

count($new_temp); // get 1

Upvotes: 0

Karthik N
Karthik N

Reputation: 951

This will give you the count 1

$temp[] = Array
(
    [@url] => url
    [@type] => image/jpeg
    [@expression] => full
    [@width] => 644
    [@height] => 429
)

$count_total = count($temp);

Upvotes: 1

Hardik
Hardik

Reputation: 439

     $temp = Array
     (
         [0] => Array
             (
            [@url] => url1
            [@type] => image/jpeg
            [@expression] => full
            [@width] => 800
            [@height] => 621
        )
)

 $count_total = count($temp);

if your array is above, you can get count as 1;

Upvotes: 0

Arun
Arun

Reputation: 3721

The array below(given by you)

$temp = Array
(
[@url] => url
[@type] => image/jpeg
[@expression] => full
[@width] => 644
[@height] => 429
)

have 5 values with indexes @url, @type, @expression, @width, @height. So it always will give you the count 5.

If you want to get count 1. You have to do like below

$temp = Array(
    array(
        [@url] => url
        [@type] => image/jpeg
        [@expression] => full
        [@width] => 644
        [@height] => 429
    )
);

Here count($temp) will give you output 1

Upvotes: 2

Related Questions