Neuron-PHP

ArrayHelper
in package

Comprehensive array manipulation utility class for the Neuron framework.

Use Neuron\Core\NArray instead for object-oriented array operations

Tags
see
NArray

This static helper class provides essential array operations and utilities for common data manipulation tasks throughout the framework. It offers safe array access methods, element searching, validation, and modification operations with proper error handling and type safety.

DEPRECATED: This class is deprecated in favor of the new NArray class in the Core component, which provides the same functionality with an object-oriented approach, method chaining, and additional features.

Key features:

  • Safe array element access with default values
  • Key existence checking and validation
  • Element searching and indexing operations
  • Array modification methods (add, remove elements)
  • Value containment checking with optional key filtering
  • Consistent null-safe operations throughout

All methods are static for convenient usage without instantiation, making them ideal for utility functions across the application.

example
// OLD (Deprecated): Static ArrayHelper usage
$config = ['host' => 'localhost', 'port' => 3306];
$host = ArrayHelper::getElement($config, 'host', 'default');     // 'localhost'
$timeout = ArrayHelper::getElement($config, 'timeout', 30);     // 30 (default)

// NEW: Use NArray instead
use Neuron\Core\NArray;
$config = new NArray(['host' => 'localhost', 'port' => 3306]);
$host = $config->getElement('host', 'default');                  // 'localhost'
$timeout = $config->getElement('timeout', 30);                   // 30 (default)

// OLD: Key and value checking
if (ArrayHelper::hasKey($config, 'database')) {
    // Handle database configuration
}

// NEW: Object-oriented approach with method chaining
if ($config->hasKey('database')) {
    // Handle database configuration
}

// OLD: Array manipulation
$users = ['alice', 'bob', 'charlie'];
$index = ArrayHelper::indexOf($users, 'bob');        // 1
ArrayHelper::remove($users, 'bob');                  // ['alice', 'charlie']

// NEW: Fluent interface with method chaining
$users = new NArray(['alice', 'bob', 'charlie']);
$index = $users->indexOf('bob');                     // 1
$filtered = $users->remove('bob');                   // NArray(['alice', 'charlie'])

Table of Contents

Methods

contains()  : bool
getElement()  : mixed|null
hasKey()  : bool
indexOf()  : mixed
remove()  : bool

Methods

contains()

public static contains(array<string|int, mixed> $data, mixed $value[, mixed $key = null ]) : bool
Parameters
$data : array<string|int, mixed>
$value : mixed
$key : mixed = null
Return values
bool

getElement()

public static getElement(array<string|int, mixed> $data, mixed $key[, null $default = null ]) : mixed|null
Parameters
$data : array<string|int, mixed>
$key : mixed
$default : null = null
Return values
mixed|null

hasKey()

public static hasKey(array<string|int, mixed> $data, mixed $key) : bool
Parameters
$data : array<string|int, mixed>
$key : mixed
Return values
bool

indexOf()

public static indexOf(array<string|int, mixed> $data, mixed $item) : mixed
Parameters
$data : array<string|int, mixed>
$item : mixed

remove()

public static remove(array<string|int, mixed> &$data, mixed $item) : bool
Parameters
$data : array<string|int, mixed>
$item : mixed
Return values
bool

        
On this page

Search results