0

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?

3
  • 6
    how can it be 1 if it has 5 elements? Commented Jul 15, 2016 at 11:35
  • @gontrollez I want to make it 1 Commented Jul 15, 2016 at 11:38
  • Count "only" counts the first "level" of the array, unless you use COUNT_RECURSIVE - A smarter way would be to use empty(); on the first one and then set count to 1 if it isn't empty. Commented Jul 15, 2016 at 11:40

6 Answers 6

2

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

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

Comments

1

This will give you the count 1

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

$count_total = count($temp);

Comments

0
     $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;

Comments

0

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

Comments

0
$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.

Comments

0

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.

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.