john john john john - 2 months ago 6x
PHP Question

how to count array which has the same attribute

I have an array like below. What I want is to echo a word according to number of

items. in this case the word must be repeated 4 times.
the sudo code is sth like this:

$my[0]['title']='first title';
$my[1]['title']='second title';
$my[2]['title']='third title';
$my[3]['title']='forth title';

echo 'this is test';


Your test data would work fine with a foreach loop, but assuming that your array can contains rows that don't have a title key, you can use array_column:

$count = count(array_column($my, 'title'));

for($i=0; $i<$count; $i++) {
    echo 'this is test';