Go to Sign up
Note: Your files never leave your device. We don't upload, transfer, or store your data.
|
|
|
|
|---|---|---|
|
|
|
PHP is a server-side scripting language designed for web development. It powers 77% of websites with a known server-side language, including WordPress, Drupal, and Laravel applications.
<?php
$greeting = "Hello, World!";
echo $greeting;
An array is PHP's most versatile data structure. It holds key-value pairs and supports both indexed and associative access.
$fruits = ['Apple', 'Banana', 'Cherry'];
echo $fruits[0]; // "Apple"
echo count($fruits); // 3
$person = [
'name' => 'Alice',
'age' => 30,
'city' => 'New York',
];
echo $person['name']; // "Alice"
$users = [
['name' => 'Alice', 'age' => 30],
['name' => 'Bob', 'age' => 25],
['name' => 'Charlie', 'age' => 35],
];
echo $users[1]['name']; // "Bob"
[] vs Long Syntax array() | Syntax | Version | Example |
|---|---|---|
Short [] | PHP 5.4+ | ['name' => 'Alice'] |
Long array() | All versions | array('name' => 'Alice') |
Short syntax is the modern standard. Use long syntax only for PHP 5.3 or legacy codebases.
<?php
$rows = [];
$handle = fopen('data.csv', 'r');
$headers = fgetcsv($handle);
while (($row = fgetcsv($handle)) !== false) {
$rows[] = array_combine($headers, $row);
}
fclose($handle);
print_r($rows);
| Aspect | fgetcsv() |
|---|---|
| When to use | Dynamic CSV files at runtime |
| Requires | File path or uploaded file at runtime |
| Flexibility | Handles any CSV size |
| Dependencies | CSV file must be present at runtime |
| Performance | Disk I/O on every request |
| Aspect | This Tool |
|---|---|
| When to use | Static data, seeders, fixtures, config |
| Output | Self-contained PHP array literal |
| Flexibility | Editable PHP source code |
| Dependencies | None at runtime — data is hardcoded |
| Performance | No disk I/O; array is in memory |
<?php
$csv = "Alice,30,New YorkBob,25,London";
$lines = explode("", $csv);
$data = array_map(function ($line) {
return str_getcsv($line);
}, $lines);
print_r($data);
| Use Case | Description |
|---|---|
| Laravel seeders | Populate database tables with seed data |
| PHPUnit fixtures | Define test data in test methods |
| Config files | Hardcode lookup tables in config/ files |
| Migration data | Embed reference data in database migrations |
| WordPress plugins | Define option arrays from spreadsheet data |
| Drupal modules | Create form options and taxonomy data |
| Shopify apps | Map product data to PHP arrays |
| API mocking | Hardcode response data for testing |
| Data entry | Convert spreadsheet data to PHP quickly |
| Documentation | Include sample datasets in PHP docs |
After loading CSV data, an interactive spreadsheet grid lets you edit before converting:
| Operation | Description |
|---|---|
| Transpose | Swap rows and columns |
| Clear | Remove all data |
| Delete Empty | Remove empty rows/columns |
| Deduplicate | Remove duplicate rows |
| Replace | Find and replace (with regex support) |
| Case transform | UPPERCASE, lowercase, Title Case |
| Insert/delete | Right-click for row/column operations |
| First Row as Header | Toggle header treatment |
All processing runs entirely in your browser. No CSV data is uploaded to any server.
Choose one of two input methods:
Upload a file: Click "Choose File" and select a .csv file.
Paste data: Click "Enter Data" to switch to the code editor. Paste your CSV.
Use the toolbar to clean your data before converting.
Toggle this ON to use the first CSV row as associative array keys.
Click Convert. The PHP array code appears in the "Output Data" panel.
Click Copy to Clipboard to paste into your .php file.
Premium users can click Download File to save.
Input CSV:
name,department,salary,active
Alice,Engineering,95000,Yes
Bob,Marketing,78000,No
Charlie,Engineering,102000,Yes
Diana,Design,85000,Yes
Configuration:
First Row as Header: ON
Output:
<?php
$array = [
['name' => 'Alice','department' => 'Engineering','salary' => '95000','active' => 'Yes',],
['name' => 'Bob','department' => 'Marketing','salary' => '78000','active' => 'No',],
['name' => 'Charlie','department' => 'Engineering','salary' => '102000','active' => 'Yes',],
['name' => 'Diana','department' => 'Design','salary' => '85000','active' => 'Yes',],
];
Input CSV:
Alice,30,New York
Bob,25,London
Charlie,35,Tokyo
Configuration:
First Row as Header: OFF
Output:
<?php
$array = [
['Alice', '30', 'New York'],
['Bob', '25', 'London'],
['Charlie', '35', 'Tokyo'],
];
<?php
namespace DatabaseSeeders;
use IlluminateSupportFacadesDB;
use IlluminateDatabaseSeeder;
class ProductSeeder extends Seeder
{
public function run(): void
{
$products = [
['name' => 'Laptop', 'price' => '1299.99', 'stock' => '45'],
['name' => 'Phone', 'price' => '699.99', 'stock' => '120'],
['name' => 'Tablet', 'price' => '499.99', 'stock' => '0'],
['name' => 'Monitor', 'price' => '399.99', 'stock' => '30'],
];
DB::table('products')->insert($products);
}
}
<?php
namespace TestsUnit;
use PHPUnitFrameworkTestCase;
class UserValidationTest extends TestCase
{
private array $testUsers;
protected function setUp(): void
{
$this->testUsers = [
['name' => 'Alice', 'email' => '[email protected]', 'age' => '30'],
['name' => 'Bob', 'email' => '[email protected]', 'age' => '25'],
['name' => 'Invalid', 'email' => 'not-an-email', 'age' => '-1'],
];
}
public function test_valid_emails(): void
{
$validUsers = array_filter($this->testUsers, function ($user) {
return filter_var($user['email'], FILTER_VALIDATE_EMAIL);
});
$this->assertCount(2, $validUsers);
}
}
<?php
// config/countries.php
return [
['code' => 'US', 'name' => 'United States', 'currency' => 'USD'],
['code' => 'GB', 'name' => 'United Kingdom', 'currency' => 'GBP'],
['code' => 'JP', 'name' => 'Japan', 'currency' => 'JPY'],
['code' => 'DE', 'name' => 'Germany', 'currency' => 'EUR'],
['code' => 'FR', 'name' => 'France', 'currency' => 'EUR'],
];
// Usage: config('countries')
<?php
$shipping_zones = [
['zone' => 'Domestic', 'rate' => '5.99', 'free_above' => '50'],
['zone' => 'Europe', 'rate' => '12.99', 'free_above' => '100'],
['zone' => 'Worldwide', 'rate' => '24.99', 'free_above' => '200'],
];
foreach ($shipping_zones as $zone) {
add_option('shipping_' . strtolower($zone['zone']), $zone);
}
<?php
use IlluminateDatabaseMigrationsMigration;
use IlluminateSupportFacadesDB;
return new class extends Migration
{
public function up(): void
{
$roles = [
['name' => 'admin', 'label' => 'Administrator'],
['name' => 'editor', 'label' => 'Editor'],
['name' => 'viewer', 'label' => 'Viewer'],
];
foreach ($roles as &$role) {
$role['created_at'] = now();
$role['updated_at'] = now();
}
DB::table('roles')->insert($roles);
}
};
No. All conversion happens locally in your browser using JavaScript. Your CSV data never leaves your device.
The short array syntax [] requires PHP 5.4+. For PHP 5.3 compatibility, replace [] with array().
Indexed arrays use numeric keys ($arr[0]). Associative arrays use string keys ($arr['name']). Enable "First Row as Header" to generate associative arrays; disable for indexed arrays.
Yes. Paste the generated array directly into seeders, migrations, config files, or test files. The short syntax [] is compatible with all Laravel versions.
fgetcsv() reads CSV files at runtime from disk. This tool generates PHP source code with hardcoded array literals. Use this tool for seed data, fixtures, and config; use fgetcsv() for dynamic CSV processing.
Yes. The built-in table editor lets you modify cells, transpose, deduplicate, find/replace, change case, and insert/delete rows and columns.
No. All values are output as PHP strings. Use PHP type casting ((int), (float), filter_var()) to convert types after generating the array.
Standard CSV with comma delimiters. The first row can be used as associative array keys (enable "First Row as Header").