3

I have this array:

Array
(
    [boks_1] => Array
        (
            [tittel] => Test
            [innhold] =>      This is a test text
            [publish] => 2
        )

    [boks_2] => Array
        (
            [tittel] => Test 3
            [innhold] => This is a text test
            [publish] => 1
        )

    [boks_3] => Array
        (
            [tittel] => Kontakt oss
            [innhold] => This is a test text
            [publish] => 1
        )
)

How can I use PHP count() to count how many times [publish] => 1 appears in my array? I am going to use the value to control the width of divs in a flexbox container.

1
  • You can't simply use the count() function, you will need to loop through the array and increment a counter. Commented Apr 7, 2016 at 15:35

3 Answers 3

6

For fun:

$count = array_count_values(array_column($array, 'publish'))[1];
  • Get an array of the publish keys
  • Count the values
  • Get the count of the 1s using index [1]

O.K. more fun:

$count = count(array_keys(array_column($array, 'publish'), 1));
  • Get an array of the publish keys
  • Get array keys where the value is 1
  • Count the array

NOTE: You might want to pass true as the third argument to array_keys() to be more accurate and use '1' instead of 1 if the 1s are strings and not integers.

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

5 Comments

Just a question, what is the , 1) in array_count_values doing? I don't see it in the documentation as having a second parameter? That supposed to be part of the array_column() I think?
So array_column($array, 'publish', 1) instead of array_column($array, 'publish'), 1)?'
@Rasclatt: That was a typo, deleted.
Yeah I figured as much, but I thought I would ask incase it was some legacy thing that I didn't know about, I learn a lot from other peoples answers, like array_column() for instance, I normally use a loop, to accomplish the same task, so now I know better!
Added more fun since you called me back here :-) That may have been where the , 1) came from earlier.
3
$newArray = array_filter($booksArray, function($bookDet) { if($bookDet["publish"]==1) { return $bookDet; } });
$getCount = count($newArray);

use array_filter to filter out only required array details, and get a count of it.

this could be the simplest, and performance oriented as well, as it won't loop.

2 Comments

you have double $ sign before newArray var, which will return 0(zero) always as count..
Nice with array_filter(), but == will return for true and some strings. Maybe function($v) { return $v['publish'] === 1; } or if string === '1'.
1

this should solve your problem :

$array = array(); //This is your data sample

$counter = 0; //This is your counter
foreach ($array as $key => $elem) {
    if (array_key_exists('publish', $elem) && $elem['publish'] === 1) {
        $counter += $elem['publish'];
    }
}

hope this'll help,

2 Comments

went wrong with the question, OP is asking particular count of publish==1
yeah i forgot the condition, so i updated ;). But your solution is cleaner than mine.

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.