0

I have an array ($myArray) which looks like this:

Array ( 
     [0] => 2001
     [1] => 2002
     [2] => 2003
     [3] => 2004
     [4] => 2005
     [5] => 2006
     [6] => 2007

etc etc

When I use

$occurences = array_count_values($myArray)

I get

Array ( 
   [2001] => 5 
   [2002] => 7 
   [2003] => 7 
   [2004] => 7 
   [2005] => 7 
   [2006] => 2 
   [2007] => 6 )

When I try to index using

echo $occurences[0];

I get an error of

Undefined offset: 0

2
  • Your original array isn't what you think it is. It might be getting changed before you try to access its values. Commented Aug 3, 2017 at 15:34
  • you are counting occurences of the values, which returns an associative array, each key being the value, and as value the number of times it is found. So it is not a numeric index array. A bit confusing here because your values are integers Commented Aug 3, 2017 at 15:41

4 Answers 4

1

try using foreach() if you want to loop over your ocurrences array :

foreach($occurences as $item) {
   echo $item . '<br>';
}

but if you want only the first element, you could use reset($occurences)

reset() gives you the first value of the array if you have an element inside the array, it also gives you FALSE in case the array is empty.

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

Comments

1

The keys in the result of array_count_values are the values in the input array. So the [0] key you're looking for isn't there. You can see in the output

Array ( 
   [2001] => 5 
   [2002] => 7 
   [2003] => 7 ...

that the first key is [2001].

If you want to get the first item, either explicitly refer to $occurrences[2001], or use

echo reset($occurrences);

instead if you don't know what the first year will be.

If you want more than just the first element, $occurrences can also be iterated with foreach.

foreach ($occurrences as $year => $count) { ...

Comments

0

There is no key name '0' in $occurences.

As you can see in the print_r of $occurences in your question, the keys of that array are the values from $myarray.

If you want to print the first value of $occurences, you should write :

echo $occurences['2001'];

Comments

0

If you read the array_count_values documentation it tells you that the function returns an associative array.

Returns an associative array of values from array as keys and their count as value.

So you cannot call the $occurences array like that.

So from what you explained above, you have the following:

<?php
 $myarray = array ( 
   0 => 2001, 
   1 => 2002,
   2 => 2003,
   3 => 2004, 
   4 => 2005, 
   5 => 2006, 
   6 => 2007 );
 
$occurences = array_count_values($myarray);

print_r($occurences);

This gives me the following:

Array ( [2001] => 1 
        [2002] => 1 
        [2003] => 1 
        [2004] => 1 
        [2005] => 1 
        [2006] => 1 
        [2007] => 1 )

Obviously, the values will differ as I guess you have a larger data set in $myarray.

If you want to get the value of each of the years, you would do echo $occurrences['2003']; which will give you back the value of the year.

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.