Download the PHP package itools/smartarray without Composer

On this page you can find all versions of the php package itools/smartarray. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package smartarray

SmartArray: Enhanced Arrays with Chainable Methods and Automatic HTML Encoding

SmartArray extends PHP arrays with automatic HTML encoding and chainable utility methods. It preserves familiar array syntax while adding powerful features for filtering, mapping, and data manipulation - making common array operations simpler, safer, and more expressive.

Table of Contents

Quick Start

Install via Composer:

Include the Composer autoloader and SmartArray class:

Convert an array to a SmartArray:

See the Method Reference for more information on available methods.

Usage Examples

Highlighting Recent Articles with position()

The position() method returns an element's position within its parent array (starting from 1), making it easy to give special treatment to important items like featured articles based on their position:

Output:

Key Features:

Common Use Cases:

Accessing Elements by Position with nth()

The nth() method provides a convenient way to access elements by their position, supporting both positive and negative indices. This is particularly useful for accessing specific items in ordered lists like search results, leaderboards, or recent activity feeds.

Key Features:

Looking Up Authors by ID with indexBy()

When working with collections of records, you often need to look up specific records by their ID or another unique field. The indexBy() method makes this easy by creating an associative array using a specified field as the key:

Output:

Important Notes:

  • If multiple records have the same key value, later records will overwrite earlier ones
  • For collections with duplicate keys, use groupBy() instead to preserve all records
  • Keys are automatically converted to strings (as per PHP array key behavior)
  • Missing keys in the source data will be skipped with a warning

Need to preserve duplicate keys? Consider using groupBy() instead.

Organizing Books by Genre with groupBy()

When working with data that has multiple items sharing the same key (like books by genre, products by category, or employees by department), groupBy() helps organize them into logical groups while preserving all records:

Output:

Common Use Cases for groupBy():

Important Notes:

Building Safe MySQL ID Lists with pluck(), map(), and implode()

When working with database records, you may need to create a comma-separated list of IDs for use in SQL IN clauses. Here's how SmartArray simplifies this process.

Creating Grid Layouts with isFirst(), isLast(), isMultipleOf() and chunk()

SmartArray makes it easy to create grid layouts by providing methods like isFirst(), isLast(), and isMultipleOf() for position-based operations. Here's a simple example creating a table layout:

Or using the chunk() method which splits the array into smaller arrays of a specified size:

Both approaches produce the same output:

Note how special characters in the data (apostrophes, quotes, ampersands) are automatically HTML encoded by SmartString to prevent XSS attacks while the table structure remains intact. See the Method Reference for more information on available methods.

Debugging and Help

SmartArray provides helpful debugging tools to inspect your data structures and explore available methods.

Debug View Call print_r() on any SmartArray to see a detailed view of its structure:

The output shows the nested structure and metadata for each level:

Interactive Help

Need a quick reference? Call help() on any SmartArray object to see available methods and usage examples:

Method Reference

Note: All methods return a new SmartArray object unless otherwise specified.

Category Method Description
Basic Usage SmartArray::new($array) Create a new SmartArray. Values stay as-is, nested arrays become SmartArrays
SmartArray::newSS($array) Create a new SmartArray. Values become HTML-encoded SmartStrings, nested arrays become SmartArrays
Value Access $obj['key'] Get a value using array syntax
$obj->key Get a value using object syntax
get($key) Get a value using method syntax
get($key, $default) Get a value with optional default if key not found
first() Get the first element
last() Get the last element
nth($index) Get element by position, ignoring keys (0=first, -1=last)
SmartArray::rawValue($value) Converts SmartArray and SmartString objects to their original values while leaving other types unchanged
Array Information count() Get the number of elements
isEmpty() Returns true if array has no elements
isNotEmpty() Returns true if array has any elements
Position & Layout isFirst() Returns true if first element in parent array
isLast() Returns true if last element in parent array
position() Gets position in parent array (starting from 1)
isMultipleOf($value) Returns true if position is multiple of value (useful for grids)
chunk($size) Splits array into smaller arrays of the specified size (for grid layouts)
Sorting & Filtering sort() Sorts elements by value (flat arrays only)
sortBy($column) Sorts rows by column value (nested arrays only)
unique() Removes duplicate values (flat arrays only)
filter() Removes falsey values ("", 0, empty array, etc)
filter($callback) Removes elements where callback returns false (callback receives raw values)
where($conditions) Removes rows not matching conditions like ['status' => 'active']
Array Transformation toArray() Converts back to regular PHP array with original values
keys() Gets array of keys, discarding the values
values() Gets array of values, discarding the keys
indexBy($column) Indexes rows by column value, latest is kept if duplicates
groupBy($column) Groups rows by column value, preserving duplicates
pluck($key) Gets array of column values from rows
pluckNth($key) Gets array of values at position from rows
implode($separator) Joins elements with separator into string
map($callback) Transforms each element using callback (callback receives raw values)
merge(...$arrays) Merges with one or more arrays. Numeric keys are renumbered, string keys are overwritten by later values.
Database Operations The following optional methods may be available when using SmartArray with database results
mysqli() Get an array of all mysqli result metadata (set when creating array from DB result)
mysqli($key) Get specific mysqli result metadata (errno, error, affected_rows, insert_id, etc)
load() Loads related record(s) if available for column
Debugging and Help
help() Displays help information about available methods
debug() Show content of object as well as properties
print_r($obj) Show array contents of object (useful for debugging)

See Also: For working with SmartArray values, check out the included companion library SmartString, all SmartArray values are SmartString objects with automatic HTML encoding and chainable methods:
https://github.com/interactivetools-com/SmartString?tab=readme-ov-file#method-reference

Questions?

This library was developed for CMS Builder, post a message in our "CMS Builder" forum here: https://www.interactivetools.com/forum/


All versions of smartarray with dependencies

PHP Build Version
Package Version
Requires php Version ^8.0
itools/smartstring Version ^v2.0.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package itools/smartarray contains the following files

Loading the files please wait ....