-
Notifications
You must be signed in to change notification settings - Fork 1
array_has
Rose Cox edited this page Mar 9, 2017
·
3 revisions
The array_has()
function walks through the given array or ArrayAccess
object to check if the specified element or offset exists. You can use dot notation to drill down to the specific key/offset depth that you need to check.
Syntax:
mixed array_add(
ArrayAccess|array $subjectArrayOrObject,
string $dotNotationKeys
);
Array examples:
$user = array(
'user_id' => 504,
'name' => 'Bob Jones',
'social' => array(
'twitter' => '@bobjones',
),
);
if ( array_has( $user, 'social.website' ) ) {}
// returns false
if ( array_has( $user, 'social.true' ) ) {}
// returns true
if ( array_has( $user, 'email' ) ) {}
// returns false
if ( array_has( $user, 'name' ) ) {}
// returns true
ArrayAccess examples, where User
implements ArrayAccess
in its class blueprint:
$user = new User ( array(
'user_id' => 504,
'name' => 'Bob Jones',
'social' => array(
'twitter' => '@bobjones',
),
) );
if ( array_has( $user, 'social.website' ) ) {}
// returns false
if ( array_has( $user, 'social.true' ) ) {}
// returns true
if ( array_has( $user, 'email' ) ) {}
// returns false
if ( array_has( $user, 'name' ) ) {}
// returns true