Currently seeking new career opportunities in web development, particularly with Laravel, Hire Me

Checking all array keys are the same with PHP

David Carr

Tutorials PHP & MySQL

Here is a simple way to check the values of an array contain the same value using array_count_values and count if all keys are the same this should equal 1. In addition to this if you would like to check for a particular value add a final comparison to the if statement:

$items = array('A','A','A');

if(count(array_count_values($items)) == 1 && $items[0] == 'A'){
    echo 'All items are the same';
} else {
    echo 'Not all the same';
}

 

Laravel Modules Your Logo Your Logo Your Logo

Become a sponsor

Help support the blog so that I can continue creating new content!

Sponsor

My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Learn Laravel with Laracasts

Faster Laravel Hosting

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.