1. Go to this page and download the library: Download itools/smartarray library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
itools / smartarray example snippets
tools\SmartArray\SmartArray;
$records = [
['id' => 10, 'name' => "John O'Connor", 'city' => 'New York'],
['id' => 15, 'name' => 'Xena "X" Smith', 'city' => 'Los Angeles'],
['id' => 20, 'name' => 'Tom & Jerry', 'city' => 'Vancouver'],
];
$users = SmartArray::new($records); // Convert to SmartArray (values get converted to SmartStrings)
// Foreach over a SmartArray just like a regular array
foreach ($users as $user) {
echo "Name: {$user['name']}, "; // use regular array syntax
echo "City: $user->city\n"; // or cleaner object syntax (no curly braces needed)
}
// Values are automatically HTML-encoded in string contexts to prevent XSS (see SmartString docs more details)
echo $users->first()->name; // Output: John O'Connor
// Use chainable methods to transform data
$userIdAsCSV = $users->pluck('id')->join(', '); // Output: "10, 15, 20"
// Easily convert back to arrays and original values
$usersArray = $users->toArray(); // Convert back to a regular PHP array and values
// Convert SmartStrings back to original values
// Note: The `value()` method returns the raw value from a `SmartString` object.
$userId = $users->first()->id->value(); // Returns 10 as an integer
$articles = [
['title' => 'Astronomers Photograph Distant Galaxy for First Time'],
['title' => 'New Species of Butterfly Found in Amazon Rainforest'],
['title' => 'Ocean Expedition Maps Unexplored Deep Sea Valleys'],
['title' => 'Ancient Star Charts Discovered in Mountain Cave'],
['title' => 'Rare Rainbow Clouds Spotted in Nordic Skies'],
['title' => 'Desert Expedition Reveals Hidden Oasis Ecosystem'],
['title' => 'Mountain Observatory Captures Meteor Shower Images']
];
$news = SmartArray::new($articles);
// Create a news listing with featured articles
echo "<div class='news-list'>\n";
foreach ($news as $article) {
// First 3 articles get heading treatment
if ($article->position() <= 3) {
echo "<h1>$article->title</h1>\n";
} else {
// Remaining articles as regular text
echo "$article->title<br>\n";
}
}
echo "</div>\n";
$topSellers = [
['rank' => 1, 'title' => 'The Great Gatsby', 'sales' => 25000000],
['rank' => 2, 'title' => '1984', 'sales' => 20000000],
['rank' => 3, 'title' => 'To Kill a Mockingbird', 'sales' => 18000000],
['rank' => 4, 'title' => 'The Catcher in the Rye', 'sales' => 15000000],
['rank' => 5, 'title' => 'The Hobbit', 'sales' => 14000000]
];
$books = SmartArray::new($topSellers);
// Get specific positions (0-based indexing)
echo $books->nth(0)->title; // "The Great Gatsby" (first book)
echo $books->nth(2)->title; // "To Kill a Mockingbird" (third book)
// Use negative indices to count from the end
echo $books->nth(-1)->title; // "The Hobbit" (last book)
echo $books->nth(-2)->title; // "The Catcher in the Rye" (second-to-last)
// Common use cases:
// Get podium finishers in a competition
$goldMedalist = $results->nth(0);
$silverMedalist = $results->nth(1);
$bronzeMedalist = $results->nth(2);
// Display recent activity with the newest first
$mostRecent = $activities->nth(0);
$secondMostRecent = $activities->nth(1);
// Show last few items in a feed
$latestLogEntry = $log->nth(-1);
$secondLatestEntry = $log->nth(-2);
$authors = [
['author_id' => 101, 'name' => 'Jane Austen', 'genre' => 'Literary Fiction'],
['author_id' => 102, 'name' => 'George Orwell', 'genre' => 'Political Fiction'],
['author_id' => 103, 'name' => 'Isaac Asimov', 'genre' => 'Science Fiction'],
['author_id' => 104, 'name' => 'Agatha Christie','genre' => 'Mystery'],
];
// Create a lookup array indexed by author_id
$authorById = SmartArray::new($authors)->indexBy('author_id');
// Now you can quickly look up authors by their ID
echo $authorById[101]->name; // Output: Jane Austen
echo $authorById[103]->genre; // Output: Science Fiction
// Particularly useful when joining data from multiple sources
$articles = [
['article_id' => 1, 'title' => 'Pride and Programming', 'author_id' => 101],
['article_id' => 2, 'title' => 'Digital Dystopia', 'author_id' => 102],
['article_id' => 3, 'title' => 'Robot Psychology', 'author_id' => 103],
];
// Display articles with author information
foreach (SmartArray::new($articles) as $article) {
$author = $authorById[$article->author_id];
echo "Title: $article->title\n";
echo "By: $author->name ($author->genre)\n\n";
}
$books = [
['title' => 'Pride and Prejudice', 'author' => 'Jane Austen', 'genre' => 'Literary Fiction', 'year' => 1813],
['title' => '1984', 'author' => 'George Orwell', 'genre' => 'Science Fiction', 'year' => 1949],
['title' => 'Foundation', 'author' => 'Isaac Asimov', 'genre' => 'Science Fiction', 'year' => 1951],
['title' => 'Emma', 'author' => 'Jane Austen', 'genre' => 'Literary Fiction', 'year' => 181],
['title' => 'I, Robot', 'author' => 'Isaac Asimov', 'genre' => 'Science Fiction', 'year' => 1950],
['title' => 'Persuasion', 'author' => 'Jane Austen', 'genre' => 'Literary Fiction', 'year' => 1818],
];
// Group books by genre
$booksByGenre = SmartArray::new($books)->groupBy('genre');
// Now you can work with each genre's books separately
foreach ($booksByGenre as $genre => $relatedBooks) {
echo "\n$genre Books:\n";
echo str_repeat('-', strlen($genre) + 7) . "\n";
foreach ($relatedBooks as $book) {
echo "- $book->title ($book->year)\n";
}
}
// Group by author to analyze their work
$booksByAuthor = SmartArray::new($books)->groupBy('author');
foreach ($booksByAuthor as $author => $books) {
$years = $books->pluck('year')->values()->sort();
echo "\n$author published {$books->count()} books ({$years->first()}-{$years->last()}):\n";
foreach ($books->sortBy('year') as $book) {
echo "- $book->title ($book->year)\n";
}
}
$articles = [
['article_id' => 1, 'title' => 'Introduction to Testing', 'author_id' => 104],
['article_id' => 2, 'title' => 'Understanding Mockups', 'author_id' => 102],
['article_id' => 3, 'title' => 'Data Handling in PHP', 'author_id' => 103],
['article_id' => 4, 'title' => 'Best Practices for MySQL', 'author_id' => 104],
['article_id' => 5, 'title' => 'Advanced PHP Techniques', 'author_id' => 105],
];
// Convert ResultSet to MySQL-safe ID list in one expressive line
$authorIdCSV = SmartArray::new($articles)->pluck('author_id')->map('intval')->unique()->join(',')->ifBlank('0')->value();
// Or for better readability, the same operation can be split across multiple lines:
$authorIdCSV = SmartArray::new($articles) // Convert ResultSet to SmartArray (nested arrays become SmartArrays)
->pluck('author_id') // Extract just the author_id column: [104, 102, 103, 104, 105]
->map('intval') // Ensure all IDs are integers: [104, 102, 103, 104, 105]
->unique() // Remove duplicate IDs: [104, 102, 103, 105]
->join(',') // Create comma-separated list: "104,102,103,105" (returns SmartString)
->ifBlank('0') // Handle empty ResultSets safely with "0" (SmartString method)
->value(); // Convert SmartString to raw string value: "104,102,103,105"
// Use in MySQL query - $authorIdCSV is now "104,102,103,105" (or "0" if empty)
$sql = "SELECT * FROM authors WHERE author_id IN ($authorIdCSV)";
$records = [
['id' => 10, 'name' => "John O'Connor", 'city' => 'New York'],
['id' => 15, 'name' => 'Xena "X" Smith', 'city' => 'Los Angeles'],
['id' => 20, 'name' => 'Tom & Jerry', 'city' => 'Vancouver'],
];
$users = SmartArray::new($records);
foreach ($users as $user) {
if ($user->isFirst()) { echo "<table border='1' cellpadding='10' style='text-align: center'>\n<tr>\n"; }
echo "<td><h1>$user->name</h1>$user->city</td>\n"; // values are automatically html encoded by SmartString
if ($user->isMultipleOf(2)) { echo "</tr>\n<tr>\n"; }
if ($user->isLast()) { echo "</tr>\n</table>\n"; }
}
if ($users->isNotEmpty()) {
echo "<table border='1' cellpadding='10' style='text-align: center'>\n";
foreach ($users->chunk(2) as $row) {
echo "<tr>\n";
foreach ($row as $user) {
echo "<td><h1>$user->name</h1>$user->city</td>\n"; // values are automatically html encoded by SmartString
}
echo "</tr>\n";
}
echo "</table>\n";
}