Oihana PHP

key.php

Table of Contents

Functions

key()  : string
Returns a transformed key by optionally prepending a prefix.

Functions

key()

Returns a transformed key by optionally prepending a prefix.

key(null|string|array<string|int, mixed> $key[, string|null $prefix = '' ][, string $separator = '.' ]) : string

This function allows you to prefix a key string with another string, separated by a custom separator. If the prefix is empty or null, the original key is returned unchanged.

Parameters
$key : null|string|array<string|int, mixed>

The key to transform :

  • null returns an empty string
  • string is used directly
  • array is joined using the $separator
$prefix : string|null = ''

Optional prefix to prepend. Default is an empty string. If null or empty, the prefix is ignored.

$separator : string = '.'

The separator used both to join array keys and to separate the prefix from the key.

Tags
example
use function oihana\core\strings\key;

key('name');                  // 'name'
key('name', 'doc');           // 'doc.name'
key('name', 'doc', '::');     // 'doc::name'
key('name', 'doc', '->');     // 'doc->name'
key('name', '');              // 'name'
key('name', null);            // 'name'

key(['a', 'b'], 'doc');       // 'doc.a.b'
key(['a', 'b'], 'doc', '::'); // 'doc::a::b'
author

Marc Alcaraz

since
1.0.0
Return values
string

The transformed key. Returns an empty string if $key is null. Returns "{$prefix}{$separator}{$key}" when a prefix is provided, otherwise returns the normalized key.


        
On this page

Search results